Skip to content

Commit 7840036

Browse files
authored
chore: update lc problems (doocs#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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 4 additions & 4 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 4 additions & 4 deletions
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

Lines changed: 4 additions & 4 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 1 addition & 1 deletion
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

0 commit comments

Comments
 (0)