Skip to content

[pull] main from doocs:main #512

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Apr 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@

## Introduction

Complete solutions to LeetCode, LCOF and LCCI problems, updated daily. Please give me a [star](https://github.com/doocs/leetcode) 🌟 if you like it.
The Doocs LeetCode repository is a comprehensive collection of solutions to LeetCode questions in multiple programming languages. The repository contains solutions to LeetCode, LCOF, LCCI questions, and more in multiple programming languages.

The repository is maintained by the Doocs community, and please give us a [star](https://github.com/doocs/leetcode) 🌟 if you like it.

[中文文档](/README.md)

Expand Down
21 changes: 21 additions & 0 deletions solution/0000-0099/0014.Longest Common Prefix/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,27 @@ impl Solution {
}
```

### **PHP**

```php
class Solution {
/**
* @param String[] $strs
* @return String
*/
function longestCommonPrefix($strs) {
$rs = "";
for ($i = 0; $i < strlen($strs[0]); $i++) {
for ($j = 1; $j < count($strs); $j++) {
if ($strs[0][$i] != $strs[$j][$i]) return $rs;
}
$rs = $rs.$strs[0][$i];
}
return $rs;
}
}
```

### **...**

```
Expand Down
21 changes: 21 additions & 0 deletions solution/0000-0099/0014.Longest Common Prefix/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,27 @@ impl Solution {
}
```

### **PHP**

```php
class Solution {
/**
* @param String[] $strs
* @return String
*/
function longestCommonPrefix($strs) {
$rs = "";
for ($i = 0; $i < strlen($strs[0]); $i++) {
for ($j = 1; $j < count($strs); $j++) {
if ($strs[0][$i] != $strs[$j][$i]) return $rs;
}
$rs = $rs.$strs[0][$i];
}
return $rs;
}
}
```

### **...**

```
Expand Down
16 changes: 16 additions & 0 deletions solution/0000-0099/0014.Longest Common Prefix/Solution.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution {
/**
* @param String[] $strs
* @return String
*/
function longestCommonPrefix($strs) {
$rs = "";
for ($i = 0; $i < strlen($strs[0]); $i++) {
for ($j = 1; $j < count($strs); $j++) {
if ($strs[0][$i] != $strs[$j][$i]) return $rs;
}
$rs = $rs.$strs[0][$i];
}
return $rs;
}
}
111 changes: 61 additions & 50 deletions solution/0200-0299/0207.Course Schedule/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,13 @@

**方法一:拓扑排序**

BFS 实现。
对于本题,我们可以将课程看作图中的节点,先修课程看作图中的边,那么我们可以将本题转化为判断有向图中是否存在环。

具体地,我们可以使用拓扑排序的思想,对于每个入度为 $0$ 的节点,我们将其出度的节点的入度减 $1$,直到所有节点都被遍历到。

如果所有节点都被遍历到,说明图中不存在环,那么我们就可以完成所有课程的学习;否则,我们就无法完成所有课程的学习。

时间复杂度 $O(n + m)$,空间复杂度 $O(n + m)$。其中 $n$ 和 $m$ 分别为课程数和先修课程数。

<!-- tabs:start -->

Expand All @@ -67,7 +73,7 @@ class Solution:
g[b].append(a)
indeg[a] += 1
cnt = 0
q = deque([i for i, v in enumerate(indeg) if v == 0])
q = deque(i for i, x in enumerate(indeg) if x == 0)
while q:
i = q.popleft()
cnt += 1
Expand Down Expand Up @@ -114,36 +120,6 @@ class Solution {
}
```

### **TypeScrpt**

```ts
function canFinish(numCourses: number, prerequisites: number[][]): boolean {
let g = Array.from({ length: numCourses }, () => []);
let indeg = new Array(numCourses).fill(0);
for (let [a, b] of prerequisites) {
g[b].push(a);
++indeg[a];
}
let q = [];
for (let i = 0; i < numCourses; ++i) {
if (!indeg[i]) {
q.push(i);
}
}
let cnt = 0;
while (q.length) {
const i = q.shift();
++cnt;
for (let j of g[i]) {
if (--indeg[j] == 0) {
q.push(j);
}
}
}
return cnt == numCourses;
}
```

### **C++**

```cpp
Expand All @@ -158,15 +134,21 @@ public:
++indeg[a];
}
queue<int> q;
for (int i = 0; i < numCourses; ++i)
if (indeg[i] == 0) q.push(i);
for (int i = 0; i < numCourses; ++i) {
if (indeg[i] == 0) {
q.push(i);
}
}
int cnt = 0;
while (!q.empty()) {
int i = q.front();
q.pop();
++cnt;
for (int j : g[i])
if (--indeg[j] == 0) q.push(j);
for (int j : g[i]) {
if (--indeg[j] == 0) {
q.push(j);
}
}
}
return cnt == numCourses;
}
Expand All @@ -185,8 +167,8 @@ func canFinish(numCourses int, prerequisites [][]int) bool {
indeg[a]++
}
q := []int{}
for i, v := range indeg {
if v == 0 {
for i, x := range indeg {
if x == 0 {
q = append(q, i)
}
}
Expand All @@ -206,36 +188,65 @@ func canFinish(numCourses int, prerequisites [][]int) bool {
}
```

### **TypeScript**

```ts
function canFinish(numCourses: number, prerequisites: number[][]): boolean {
const g: number[][] = new Array(numCourses).fill(0).map(() => []);
const indeg: number[] = new Array(numCourses).fill(0);
for (const [a, b] of prerequisites) {
g[b].push(a);
indeg[a]++;
}
const q: number[] = [];
for (let i = 0; i < numCourses; ++i) {
if (indeg[i] == 0) {
q.push(i);
}
}
let cnt = 0;
while (q.length) {
const i = q.shift()!;
cnt++;
for (const j of g[i]) {
if (--indeg[j] == 0) {
q.push(j);
}
}
}
return cnt == numCourses;
}
```

### **C#**

```cs
public class Solution {
public bool CanFinish(int numCourses, int[][] prerequisites) {
var g = new List<int>[numCourses];
for (int i = 0; i < numCourses; ++i)
{
for (int i = 0; i < numCourses; ++i) {
g[i] = new List<int>();
}
var indeg = new int[numCourses];
foreach (var p in prerequisites)
{
foreach (var p in prerequisites) {
int a = p[0], b = p[1];
g[b].Add(a);
++indeg[a];
}
var q = new Queue<int>();
for (int i = 0; i < numCourses; ++i)
{
if (indeg[i] == 0) q.Enqueue(i);
for (int i = 0; i < numCourses; ++i) {
if (indeg[i] == 0) {
q.Enqueue(i);
}
}
var cnt = 0;
while (q.Count > 0)
{
while (q.Count > 0) {
int i = q.Dequeue();
++cnt;
foreach (int j in g[i])
{
if (--indeg[j] == 0) q.Enqueue(j);
foreach (int j in g[i]) {
if (--indeg[j] == 0) {
q.Enqueue(j);
}
}
}
return cnt == numCourses;
Expand Down
Loading