Skip to content

Commit 7840036

Browse files
authored
chore: update lc problems (#3314)
1 parent d5d3ad8 commit 7840036

File tree

291 files changed

+787
-747
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

291 files changed

+787
-747
lines changed

lcci/03.01.Three in One/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,17 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcci/03.01.Three%20in%20On
4747

4848
### 方法一:数组模拟
4949

50-
我们使用一个变量 $cap$ 来表示每个栈的大小,使用一个长度为 $3 \times \text{cap} + 3$ 的数组 $stk$ 来模拟三个栈,数组的前 $3 \times \text{cap}$ 个元素用来存储栈的元素,数组的后三个元素用来存储每个栈的元素个数。
50+
我们使用一个变量 $cap$ 来表示每个栈的大小,使用一个长度为 $3 \times \textit{cap} + 3$ 的数组 $stk$ 来模拟三个栈,数组的前 $3 \times \textit{cap}$ 个元素用来存储栈的元素,数组的后三个元素用来存储每个栈的元素个数。
5151

5252
对于 `push` 操作,我们首先判断栈是否已满,如果未满,则将元素压入栈中,并更新栈的元素个数。
5353

5454
对于 `pop` 操作,我们首先判断栈是否为空,如果不为空,则更新栈的元素个数,并返回栈顶元素。
5555

5656
对于 `peek` 操作,我们首先判断栈是否为空,如果不为空,则返回栈顶元素。
5757

58-
对于 `isEmpty` 操作,我们直接判断栈是否为空即可。对于栈 $i$,我们只需要判断 $stk[\text{cap} \times 3 + i]$ 是否为 $0$ 即可。
58+
对于 `isEmpty` 操作,我们直接判断栈是否为空即可。对于栈 $i$,我们只需要判断 $stk[\textit{cap} \times 3 + i]$ 是否为 $0$ 即可。
5959

60-
时间复杂度上,每个操作的时间复杂度均为 $O(1)$。空间复杂度为 $O(\text{cap})$,其中 $\text{cap}$ 为栈的大小。
60+
时间复杂度上,每个操作的时间复杂度均为 $O(1)$。空间复杂度为 $O(\textit{cap})$,其中 $\textit{cap}$ 为栈的大小。
6161

6262
<!-- tabs:start -->
6363

lcci/03.01.Three in One/README_EN.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,17 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcci/03.01.Three%20in%20On
6262

6363
### Solution 1: Array Simulation
6464

65-
We use a variable $cap$ to represent the size of each stack, and use an array $stk$ of length $3 \times \text{cap} + 3$ to simulate three stacks. The first $3 \times \text{cap}$ elements of the array are used to store the elements of the stack, and the last three elements are used to store the number of elements in each stack.
65+
We use a variable $cap$ to represent the size of each stack, and use an array $stk$ of length $3 \times \textit{cap} + 3$ to simulate three stacks. The first $3 \times \textit{cap}$ elements of the array are used to store the elements of the stack, and the last three elements are used to store the number of elements in each stack.
6666

6767
For the `push` operation, we first check whether the stack is full. If it is not full, we push the element into the stack and update the number of elements in the stack.
6868

6969
For the `pop` operation, we first check whether the stack is empty. If it is not empty, we update the number of elements in the stack and return the top element of the stack.
7070

7171
For the `peek` operation, we first check whether the stack is empty. If it is not empty, we return the top element of the stack.
7272

73-
For the `isEmpty` operation, we directly check whether the stack is empty. For stack $i$, we only need to check whether $stk[\text{cap} \times 3 + i]$ is $0$.
73+
For the `isEmpty` operation, we directly check whether the stack is empty. For stack $i$, we only need to check whether $stk[\textit{cap} \times 3 + i]$ is $0$.
7474

75-
In terms of time complexity, the time complexity of each operation is $O(1)$. The space complexity is $O(\text{cap})$, where $\text{cap}$ is the size of the stack.
75+
In terms of time complexity, the time complexity of each operation is $O(1)$. The space complexity is $O(\textit{cap})$, where $\textit{cap}$ is the size of the stack.
7676

7777
<!-- tabs:start -->
7878

lcci/04.02.Minimum Height Tree/README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcci/04.02.Minimum%20Heigh
2424

2525
### 方法一:递归
2626

27-
我们设计一个函数 $\text{dfs}(l, r)$,表示构造出从 $l$ 到 $r$ 的子树,那么答案就是 $\text{dfs}(0, \text{len}(nums) - 1)$。
27+
我们设计一个函数 $\textit{dfs}(l, r)$,表示构造出从 $l$ 到 $r$ 的子树,那么答案就是 $\textit{dfs}(0, \textit{len}(nums) - 1)$。
2828

29-
函数 $\text{dfs}(l, r)$ 的执行过程如下:
29+
函数 $\textit{dfs}(l, r)$ 的执行过程如下:
3030

31-
1. 如果 $l > r$,返回 $\text{None}$。
32-
2. 否则,我们计算出中间位置 $mid = \frac{l + r}{2}$,然后构造出根节点,左子树为 $\text{dfs}(l, mid - 1)$,右子树为 $\text{dfs}(mid + 1, r)$。
31+
1. 如果 $l > r$,返回 $\textit{None}$。
32+
2. 否则,我们计算出中间位置 $mid = \frac{l + r}{2}$,然后构造出根节点,左子树为 $\textit{dfs}(l, mid - 1)$,右子树为 $\textit{dfs}(mid + 1, r)$。
3333
3. 最后返回根节点。
3434

3535
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组的长度。

lcci/04.05.Legal Binary Search Tree/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcci/04.05.Legal%20Binary%
2626

2727
我们可以对二叉树进行递归中序遍历,如果遍历到的结果是严格升序的,那么这棵树就是一个二叉搜索树。
2828

29-
因此,我们使用一个变量 $\textit{prev}$ 来保存上一个遍历到的节点,初始时 $\textit{prev} = -\infty$,然后我们递归遍历左子树,如果左子树不是二叉搜索树,直接返回 $\text{False}$,否则判断当前节点的值是否大于 $\textit{prev}$,如果不是,返回 $\text{False}$,否则更新 $\textit{prev}$ 为当前节点的值,然后递归遍历右子树。
29+
因此,我们使用一个变量 $\textit{prev}$ 来保存上一个遍历到的节点,初始时 $\textit{prev} = -\infty$,然后我们递归遍历左子树,如果左子树不是二叉搜索树,直接返回 $\textit{False}$,否则判断当前节点的值是否大于 $\textit{prev}$,如果不是,返回 $\textit{False}$,否则更新 $\textit{prev}$ 为当前节点的值,然后递归遍历右子树。
3030

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

lcci/05.02.Binary Number to String/README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcci/05.02.Binary%20Number
4242

4343
$$
4444
\begin{aligned}
45-
0.8125 \times 2 &= 1.625 \quad \text{取整数部分} \quad 1 \\
46-
0.625 \times 2 &= 1.25 \quad \text{取整数部分} \quad 1 \\
47-
0.25 \times 2 &= 0.5 \quad \text{取整数部分} \quad 0 \\
48-
0.5 \times 2 &= 1 \quad \text{取整数部分} \quad 1 \\
45+
0.8125 \times 2 &= 1.625 \quad \textit{取整数部分} \quad 1 \\
46+
0.625 \times 2 &= 1.25 \quad \textit{取整数部分} \quad 1 \\
47+
0.25 \times 2 &= 0.5 \quad \textit{取整数部分} \quad 0 \\
48+
0.5 \times 2 &= 1 \quad \textit{取整数部分} \quad 1 \\
4949
\end{aligned}
5050
$$
5151

lcci/05.02.Binary Number to String/README_EN.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ Let's take an example, suppose we want to convert $0.8125$ to a binary fraction,
5151

5252
$$
5353
\begin{aligned}
54-
0.8125 \times 2 &= 1.625 \quad \text{take the integer part} \quad 1 \\
55-
0.625 \times 2 &= 1.25 \quad \text{take the integer part} \quad 1 \\
56-
0.25 \times 2 &= 0.5 \quad \text{take the integer part} \quad 0 \\
57-
0.5 \times 2 &= 1 \quad \text{take the integer part} \quad 1 \\
54+
0.8125 \times 2 &= 1.625 \quad \textit{take the integer part} \quad 1 \\
55+
0.625 \times 2 &= 1.25 \quad \textit{take the integer part} \quad 1 \\
56+
0.25 \times 2 &= 0.5 \quad \textit{take the integer part} \quad 0 \\
57+
0.5 \times 2 &= 1 \quad \textit{take the integer part} \quad 1 \\
5858
\end{aligned}
5959
$$
6060

lcci/05.07.Exchange/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcci/05.07.Exchange/README
4444

4545
### 方法一:位运算
4646

47-
我们可以将 $\text{num}$ 与 $\text{0x55555555}$ 进行与运算,得到的结果是 $\text{num}$ 的偶数位,然后将其左移一位。再将 $\text{num}$ 与 $\text{0xaaaaaaaa}$ 进行与运算,得到的结果是 $\text{num}$ 的奇数位,然后将其右移一位。最后将两个结果进行或运算,即可得到答案。
47+
我们可以将 $\textit{num}$ 与 $\textit{0x55555555}$ 进行与运算,得到的结果是 $\textit{num}$ 的偶数位,然后将其左移一位。再将 $\textit{num}$ 与 $\textit{0xaaaaaaaa}$ 进行与运算,得到的结果是 $\textit{num}$ 的奇数位,然后将其右移一位。最后将两个结果进行或运算,即可得到答案。
4848

4949
时间复杂度 $O(1)$,空间复杂度 $O(1)$。
5050

lcci/10.09.Sorted Matrix Search/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -249,9 +249,9 @@ class Solution {
249249

250250
这里我们以左下角作为起始搜索点,往右上方向开始搜索,比较当前元素 `matrix[i][j]``target` 的大小关系:
251251

252-
- 若 $\text{matrix}[i][j] = \text{target}$,说明找到了目标值,直接返回 `true`
253-
- 若 $\text{matrix}[i][j] > \text{target}$,说明这一列从当前位置开始往上的所有元素均大于 `target`,应该让 $i$ 指针往上移动,即 $i \leftarrow i - 1$。
254-
- 若 $\text{matrix}[i][j] < \text{target}$,说明这一行从当前位置开始往右的所有元素均小于 `target`,应该让 $j$ 指针往右移动,即 $j \leftarrow j + 1$。
252+
- 若 $\textit{matrix}[i][j] = \textit{target}$,说明找到了目标值,直接返回 `true`
253+
- 若 $\textit{matrix}[i][j] > \textit{target}$,说明这一列从当前位置开始往上的所有元素均大于 `target`,应该让 $i$ 指针往上移动,即 $i \leftarrow i - 1$。
254+
- 若 $\textit{matrix}[i][j] < \textit{target}$,说明这一行从当前位置开始往右的所有元素均小于 `target`,应该让 $j$ 指针往右移动,即 $j \leftarrow j + 1$。
255255

256256
若搜索结束依然找不到 `target`,返回 `false`
257257

lcci/10.09.Sorted Matrix Search/README_EN.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -258,9 +258,9 @@ class Solution {
258258

259259
Here, we start searching from the bottom left corner and move towards the top right direction, comparing the current element `matrix[i][j]` with `target`:
260260

261-
- If $\text{matrix}[i][j] = \text{target}$, it means the target value has been found, and we directly return `true`.
262-
- If $\text{matrix}[i][j] > \text{target}$, it means all elements in this column from the current position upwards are greater than `target`, so we should move the $i$ pointer upwards, i.e., $i \leftarrow i - 1$.
263-
- If $\text{matrix}[i][j] < \text{target}$, it means all elements in this row from the current position to the right are less than `target`, so we should move the $j$ pointer to the right, i.e., $j \leftarrow j + 1$.
261+
- If $\textit{matrix}[i][j] = \textit{target}$, it means the target value has been found, and we directly return `true`.
262+
- If $\textit{matrix}[i][j] > \textit{target}$, it means all elements in this column from the current position upwards are greater than `target`, so we should move the $i$ pointer upwards, i.e., $i \leftarrow i - 1$.
263+
- If $\textit{matrix}[i][j] < \textit{target}$, it means all elements in this row from the current position to the right are less than `target`, so we should move the $j$ pointer to the right, i.e., $j \leftarrow j + 1$.
264264

265265
If the search ends and the `target` is still not found, return `false`.
266266

lcci/16.25.LRU Cache/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ When accessing a node, if the node exists, we delete it from its original positi
6767

6868
When inserting a node, if the node exists, we delete it from its original position and reinsert it at the head of the list. If it does not exist, we first check if the cache is full. If it is full, we delete the node at the tail of the list and insert the new node at the head of the list.
6969

70-
The time complexity is $O(1)$, and the space complexity is $O(\text{capacity})$.
70+
The time complexity is $O(1)$, and the space complexity is $O(\textit{capacity})$.
7171

7272
<!-- tabs:start -->
7373

lcci/17.20.Continuous Median/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ findMedian() -&gt; 2
4848

4949
### 方法一:大小根堆(优先队列)
5050

51-
我们可以使用两个堆来维护所有的元素,一个小根堆 $\text{minQ}$ 和一个大根堆 $\text{maxQ}$,其中小根堆 $\text{minQ}$ 存储较大的一半,大根堆 $\text{maxQ}$ 存储较小的一半。
51+
我们可以使用两个堆来维护所有的元素,一个小根堆 $\textit{minQ}$ 和一个大根堆 $\textit{maxQ}$,其中小根堆 $\textit{minQ}$ 存储较大的一半,大根堆 $\textit{maxQ}$ 存储较小的一半。
5252

53-
调用 `addNum` 方法时,我们首先将元素加入到大根堆 $\text{maxQ}$,然后将 $\text{maxQ}$ 的堆顶元素弹出并加入到小根堆 $\text{minQ}$。如果此时 $\text{minQ}$ 的大小与 $\text{maxQ}$ 的大小差值大于 $1$,我们就将 $\text{minQ}$ 的堆顶元素弹出并加入到 $\text{maxQ}$。时间复杂度为 $O(\log n)$。
53+
调用 `addNum` 方法时,我们首先将元素加入到大根堆 $\textit{maxQ}$,然后将 $\textit{maxQ}$ 的堆顶元素弹出并加入到小根堆 $\textit{minQ}$。如果此时 $\textit{minQ}$ 的大小与 $\textit{maxQ}$ 的大小差值大于 $1$,我们就将 $\textit{minQ}$ 的堆顶元素弹出并加入到 $\textit{maxQ}$。时间复杂度为 $O(\log n)$。
5454

55-
调用 `findMedian` 方法时,如果 $\text{minQ}$ 的大小等于 $\text{maxQ}$ 的大小,说明元素的总数为偶数,我们就可以返回 $\text{minQ}$ 的堆顶元素与 $\text{maxQ}$ 的堆顶元素的平均值;否则,我们返回 $\text{minQ}$ 的堆顶元素。时间复杂度为 $O(1)$。
55+
调用 `findMedian` 方法时,如果 $\textit{minQ}$ 的大小等于 $\textit{maxQ}$ 的大小,说明元素的总数为偶数,我们就可以返回 $\textit{minQ}$ 的堆顶元素与 $\textit{maxQ}$ 的堆顶元素的平均值;否则,我们返回 $\textit{minQ}$ 的堆顶元素。时间复杂度为 $O(1)$。
5656

5757
空间复杂度为 $O(n)$。其中 $n$ 为元素的个数。
5858

lcci/17.20.Continuous Median/README_EN.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ findMedian() -&gt; 2
5555

5656
### Solution 1: Min Heap and Max Heap (Priority Queue)
5757

58-
We can use two heaps to maintain all the elements, a min heap $\text{minQ}$ and a max heap $\text{maxQ}$, where the min heap $\text{minQ}$ stores the larger half, and the max heap $\text{maxQ}$ stores the smaller half.
58+
We can use two heaps to maintain all the elements, a min heap $\textit{minQ}$ and a max heap $\textit{maxQ}$, where the min heap $\textit{minQ}$ stores the larger half, and the max heap $\textit{maxQ}$ stores the smaller half.
5959

60-
When calling the `addNum` method, we first add the element to the max heap $\text{maxQ}$, then pop the top element of $\text{maxQ}$ and add it to the min heap $\text{minQ}$. If at this time the size difference between $\text{minQ}$ and $\text{maxQ}$ is greater than $1$, we pop the top element of $\text{minQ}$ and add it to $\text{maxQ}$. The time complexity is $O(\log n)$.
60+
When calling the `addNum` method, we first add the element to the max heap $\textit{maxQ}$, then pop the top element of $\textit{maxQ}$ and add it to the min heap $\textit{minQ}$. If at this time the size difference between $\textit{minQ}$ and $\textit{maxQ}$ is greater than $1$, we pop the top element of $\textit{minQ}$ and add it to $\textit{maxQ}$. The time complexity is $O(\log n)$.
6161

62-
When calling the `findMedian` method, if the size of $\text{minQ}$ is equal to the size of $\text{maxQ}$, it means the total number of elements is even, and we can return the average value of the top elements of $\text{minQ}$ and $\text{maxQ}$; otherwise, we return the top element of $\text{minQ}$. The time complexity is $O(1)$.
62+
When calling the `findMedian` method, if the size of $\textit{minQ}$ is equal to the size of $\textit{maxQ}$, it means the total number of elements is even, and we can return the average value of the top elements of $\textit{minQ}$ and $\textit{maxQ}$; otherwise, we return the top element of $\textit{minQ}$. The time complexity is $O(1)$.
6363

6464
The space complexity is $O(n)$, where $n$ is the number of elements.
6565

lcci/17.22.Word Transformer/README.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,15 @@ wordList = [&quot;hot&quot;,&quot;dot&quot;,&quot;dog&quot;,&quot;lot&quot;,&quo
5050

5151
我们定义一个答案数组 $\textit{ans}$,初始时只包含 $\textit{beginWord}$。然后我们定义一个数组 $\textit{vis}$,用来标记 $\textit{wordList}$ 中的单词是否被访问过。
5252

53-
接下来,我们设计一个函数 $\text{dfs}(s)$,表示从 $\textit{s}$ 出发,尝试将 $\textit{s}$ 转换为 $\textit{endWord}$,是否能够成功。如果能够成功,返回 $\text{True}$,否则返回 $\text{False}$。
53+
接下来,我们设计一个函数 $\textit{dfs}(s)$,表示从 $\textit{s}$ 出发,尝试将 $\textit{s}$ 转换为 $\textit{endWord}$,是否能够成功。如果能够成功,返回 $\textit{True}$,否则返回 $\textit{False}$。
5454

55-
函数 $\text{dfs}(s)$ 的具体实现如下:
55+
函数 $\textit{dfs}(s)$ 的具体实现如下:
5656

57-
1. 如果 $\textit{s}$ 等于 $\textit{endWord}$,说明转换成功,返回 $\text{True}$;
58-
2. 否则,我们遍历 $\textit{wordList}$ 中的每个单词 $\textit{t}$,如果 $\textit{t}$ 没有被访问过且 $\textit{s}$ 和 $\textit{t}$ 之间只有一个字符不同,那么我们将 $\textit{t}$ 标记为已访问,并将 $\textit{t}$ 加入到 $\textit{ans}$ 中,然后递归调用 $\text{dfs}(t)$,如果返回 $\text{True}$,说明转换成功,我们返回 $\text{True}$,否则我们将 $\textit{t}$ 从 $\textit{ans}$ 中移除,继续遍历下一个单词;
59-
3. 如果遍历完 $\textit{wordList}$ 中的所有单词都没有找到可以转换的单词,说明转换失败,我们返回 $\text{False}$。
57+
1. 如果 $\textit{s}$ 等于 $\textit{endWord}$,说明转换成功,返回 $\textit{True}$;
58+
2. 否则,我们遍历 $\textit{wordList}$ 中的每个单词 $\textit{t}$,如果 $\textit{t}$ 没有被访问过且 $\textit{s}$ 和 $\textit{t}$ 之间只有一个字符不同,那么我们将 $\textit{t}$ 标记为已访问,并将 $\textit{t}$ 加入到 $\textit{ans}$ 中,然后递归调用 $\textit{dfs}(t)$,如果返回 $\textit{True}$,说明转换成功,我们返回 $\textit{True}$,否则我们将 $\textit{t}$ 从 $\textit{ans}$ 中移除,继续遍历下一个单词;
59+
3. 如果遍历完 $\textit{wordList}$ 中的所有单词都没有找到可以转换的单词,说明转换失败,我们返回 $\textit{False}$。
6060

61-
最后,我们调用 $\text{dfs}(\textit{beginWord})$,如果返回 $\text{True}$,说明转换成功,我们返回 $\textit{ans}$,否则返回空数组。
61+
最后,我们调用 $\textit{dfs}(\textit{beginWord})$,如果返回 $\textit{True}$,说明转换成功,我们返回 $\textit{ans}$,否则返回空数组。
6262

6363
<!-- tabs:start -->
6464

lcci/17.23.Max Black Square/README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,16 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcci/17.23.Max%20Black%20S
5252

5353
$$
5454
down[i][j] = \begin{cases}
55-
down[i + 1][j] + 1, & matrix[i][j] = 0 \text{ 且 } i + 1 < n \\
56-
1, & matrix[i][j] = 0 \text{ 且 } i + 1 = n \\
55+
down[i + 1][j] + 1, & matrix[i][j] = 0 \textit{ 且 } i + 1 < n \\
56+
1, & matrix[i][j] = 0 \textit{ 且 } i + 1 = n \\
5757
0, & matrix[i][j] = 1
5858
\end{cases}
5959
$$
6060

6161
$$
6262
right[i][j] = \begin{cases}
63-
right[i][j + 1] + 1, & matrix[i][j] = 0 \text{ 且 } j + 1 < n \\
64-
1, & matrix[i][j] = 0 \text{ 且 } j + 1 = n \\
63+
right[i][j + 1] + 1, & matrix[i][j] = 0 \textit{ 且 } j + 1 < n \\
64+
1, & matrix[i][j] = 0 \textit{ 且 } j + 1 = n \\
6565
0, & matrix[i][j] = 1
6666
\end{cases}
6767
$$

0 commit comments

Comments
 (0)