Skip to content
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
8 changes: 4 additions & 4 deletions lcci/01.05.One Away/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ second = "pal"

### 方法一:分情况讨论 + 双指针

我们将字符串 $first$ 和 $second$ 的长度记为 $m$ 和 $n$,不妨设 $m \geq n$。
我们将字符串 $\textit{first}$ 和 $\textit{second}$ 的长度记为 $m$ 和 $n$,不妨设 $m \geq n$。

接下来分情况讨论:

- 当 $m - n \gt 1$ 时,$first$ 和 $second$ 无法通过一次编辑得到,返回 `false`;
- 当 $m = n$ 时,$first$ 和 $second$ 只有在且仅在有且仅有一个字符不同的情况下才能通过一次编辑得到;
- 当 $m - n = 1$ 时,$first$ 和 $second$ 只有在且仅在 $second$ 是 $first$ 删除一个字符后得到的情况下才能通过一次编辑得到,我们可以使用双指针来实现。
- 当 $m - n \gt 1$ 时,$\textit{first}$ 和 $\textit{second}$ 无法通过一次编辑得到,返回 `false`;
- 当 $m = n$ 时,$\textit{first}$ 和 $\textit{second}$ 只有在且仅在有且仅有一个字符不同的情况下才能通过一次编辑得到;
- 当 $m - n = 1$ 时,$\textit{first}$ 和 $\textit{second}$ 只有在且仅在 $\textit{second}$ 是 $\textit{first}$ 删除一个字符后得到的情况下才能通过一次编辑得到,我们可以使用双指针来实现。

时间复杂度 $O(n)$,其中 $n$ 为字符串长度。空间复杂度 $O(1)$。

Expand Down
12 changes: 6 additions & 6 deletions lcci/01.05.One Away/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ second = "pal"

<!-- solution:start -->

### Solution 1: Case Discussion + Two Pointers
### Solution 1: Case Analysis + Two Pointers

We denote the lengths of strings $first$ and $second$ as $m$ and $n$, respectively, where $m \geq n$.
Let the lengths of the strings $\textit{first}$ and $\textit{second}$ be $m$ and $n$, respectively. Assume $m \geq n$.

Next, we discuss different cases:
Next, we discuss the following cases:

- When $m - n > 1$, $first$ and $second$ cannot be obtained through a single edit, so we return `false`.
- When $m = n$, $first$ and $second$ can only be obtained through a single edit if and only if exactly one character is different.
- When $m - n = 1$, $first$ and $second$ can only be obtained through a single edit if and only if $second$ is obtained by deleting one character from $first$. We can use two pointers to implement this.
- When $m - n \gt 1$, $\textit{first}$ and $\textit{second}$ cannot be made equal with one edit, so return `false`;
- When $m = n$, $\textit{first}$ and $\textit{second}$ can be made equal with one edit only if there is exactly one different character;
- When $m - n = 1$, $\textit{first}$ and $\textit{second}$ can be made equal with one edit only if $\textit{second}$ is obtained by deleting one character from $\textit{first}$. We can use two pointers to achieve this.

The time complexity is $O(n)$, where $n$ is the length of the string. The space complexity is $O(1)$.

Expand Down
16 changes: 0 additions & 16 deletions lcci/01.06.Compress String/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,22 +62,6 @@ class Solution:
return min(S, t, key=len)
```

#### Python3

```python
class Solution:
def compressString(self, S: str) -> str:
t = []
i, n = 0, len(S)
while i < n:
j = i + 1
while j < n and S[j] == S[i]:
j += 1
t.append(S[i] + str(j - i))
i = j
return min(S, "".join(t), key=len)
```

#### Java

```java
Expand Down
16 changes: 0 additions & 16 deletions lcci/01.06.Compress String/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,22 +69,6 @@ class Solution:
return min(S, t, key=len)
```

#### Python3

```python
class Solution:
def compressString(self, S: str) -> str:
t = []
i, n = 0, len(S)
while i < n:
j = i + 1
while j < n and S[j] == S[i]:
j += 1
t.append(S[i] + str(j - i))
i = j
return min(S, "".join(t), key=len)
```

#### Java

```java
Expand Down
11 changes: 0 additions & 11 deletions lcci/01.06.Compress String/Solution2.py

This file was deleted.

4 changes: 2 additions & 2 deletions lcci/01.07.Rotate Matrix/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcci/01.07.Rotate%20Matrix

### 方法一:原地翻转

根据题目要求,我们实际上需要将 $matrix[i][j]$ 旋转至 $matrix[j][n - i - 1]$。
根据题目要求,我们实际上需要将 $\text{matrix}[i][j]$ 旋转至 $\text{matrix}[j][n - i - 1]$。

我们可以先对矩阵进行上下翻转,即 $matrix[i][j]$ 和 $matrix[n - i - 1][j]$ 进行交换,然后再对矩阵进行主对角线翻转,即 $matrix[i][j]$ 和 $matrix[j][i]$ 进行交换。这样就能将 $matrix[i][j]$ 旋转至 $matrix[j][n - i - 1]$ 了。
我们可以先对矩阵进行上下翻转,即 $\text{matrix}[i][j]$ 和 $\text{matrix}[n - i - 1][j]$ 进行交换,然后再对矩阵进行主对角线翻转,即 $\text{matrix}[i][j]$ 和 $\text{matrix}[j][i]$ 进行交换。这样就能将 $\text{matrix}[i][j]$ 旋转至 $\text{matrix}[j][n - i - 1]$ 了。

时间复杂度 $O(n^2)$,其中 $n$ 是矩阵的边长。空间复杂度 $O(1)$。

Expand Down
6 changes: 3 additions & 3 deletions lcci/01.07.Rotate Matrix/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,11 @@ Rotate the matrix <strong>in place. </strong>It becomes:

<!-- solution:start -->

### Solution 1: In-place Rotation
### Solution 1: In-Place Rotation

According to the problem requirements, we actually need to rotate $matrix[i][j]$ to $matrix[j][n - i - 1]$.
According to the problem requirements, we need to rotate $\text{matrix}[i][j]$ to $\text{matrix}[j][n - i - 1]$.

We can first flip the matrix upside down, that is, swap $matrix[i][j]$ and $matrix[n - i - 1][j]$, and then flip the matrix along the main diagonal, that is, swap $matrix[i][j]$ and $matrix[j][i]$. This way, we can rotate $matrix[i][j]$ to $matrix[j][n - i - 1]$.
We can first flip the matrix upside down, i.e., swap $\text{matrix}[i][j]$ with $\text{matrix}[n - i - 1][j]$, and then flip the matrix along the main diagonal, i.e., swap $\text{matrix}[i][j]$ with $\text{matrix}[j][i]$. This will rotate $\text{matrix}[i][j]$ to $\text{matrix}[j][n - i - 1]$.

The time complexity is $O(n^2)$, where $n$ is the side length of the matrix. The space complexity is $O(1)$.

Expand Down
18 changes: 18 additions & 0 deletions lcci/02.08.Linked List Cycle/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcci/02.08.Linked%20List%2

<!-- tabs:start -->

#### Python3

```python
# Definition for singly-linked list.
# class ListNode:
Expand All @@ -66,6 +68,8 @@ class Solution:
return ans
```

#### Java

```java
/**
* Definition for singly-linked list.
Expand Down Expand Up @@ -98,6 +102,8 @@ public class Solution {
}
```

#### C++

```cpp
/**
* Definition for singly-linked list.
Expand Down Expand Up @@ -129,6 +135,8 @@ public:
};
```

#### Go

```go
/**
* Definition for singly-linked list.
Expand All @@ -155,6 +163,8 @@ func detectCycle(head *ListNode) *ListNode {
}
```

#### TypeScript

```ts
/**
* Definition for singly-linked list.
Expand Down Expand Up @@ -186,6 +196,8 @@ function detectCycle(head: ListNode | null): ListNode | null {
}
```

#### JavaScript

```js
/**
* Definition for singly-linked list.
Expand Down Expand Up @@ -217,6 +229,8 @@ var detectCycle = function (head) {
};
```

#### Swift

```swift
/*
* public class ListNode {
Expand Down Expand Up @@ -251,4 +265,8 @@ class Solution {
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
90 changes: 87 additions & 3 deletions lcci/04.08.First Common Ancestor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,13 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcci/04.08.First%20Common%

<!-- solution:start -->

### 方法一
### 方法一:递归

我们首先判断根节点是否为空,或者根节点是否等于 $\textit{p}$ 或 $\textit{q}$,如果是的话,直接返回根节点。

然后递归地对左右子树进行查找,分别得到 $\textit{left}$ 和 $\textit{right}$。如果 $\textit{left}$ 和 $\textit{right}$ 都不为空,说明 $\textit{p}$ 和 $\textit{q}$ 分别在左右子树中,那么根节点就是最近公共祖先。否则,如果 $\textit{left}$ 和 $\textit{right}$ 中有一个为空,说明 $\textit{p}$ 和 $\textit{q}$ 都在非空的子树中,那么非空的子树的根节点就是最近公共祖先。

时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是二叉树中节点的数目。

<!-- tabs:start -->

Expand All @@ -41,11 +47,11 @@ class Solution:
def lowestCommonAncestor(
self, root: TreeNode, p: TreeNode, q: TreeNode
) -> TreeNode:
if root is None or root == p or root == q:
if root is None or root in [p, q]:
return root
left = self.lowestCommonAncestor(root.left, p, q)
right = self.lowestCommonAncestor(root.right, p, q)
return right if left is None else (left if right is None else root)
return root if left and right else left or right
```

#### Java
Expand All @@ -72,6 +78,84 @@ class Solution {
}
```

#### C++

```cpp
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if (!root || root == p || root == q) {
return root;
}
TreeNode* left = lowestCommonAncestor(root->left, p, q);
TreeNode* right = lowestCommonAncestor(root->right, p, q);
return left && right ? root : (left ? left : right);
}
};
```

#### Go

```go
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func lowestCommonAncestor(root *TreeNode, p *TreeNode, q *TreeNode) *TreeNode {
if root == nil || root == p || root == q {
return root
}
left := lowestCommonAncestor(root.Left, p, q)
right := lowestCommonAncestor(root.Right, p, q)
if left == nil {
return right
}
if right == nil {
return left
}
return root
}
```

#### JavaScript

```js
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @param {TreeNode} p
* @param {TreeNode} q
* @return {TreeNode}
*/
var lowestCommonAncestor = function (root, p, q) {
if (!root || root === p || root === q) {
return root;
}
const left = lowestCommonAncestor(root.left, p, q);
const right = lowestCommonAncestor(root.right, p, q);
return left && right ? root : left || right;
};
```

#### Swift

```swift
Expand Down
Loading