Skip to content

Commit 52f0f46

Browse files
authored
feat: add solutions to lc problems (doocs#2031)
1 parent ce5aaa6 commit 52f0f46

File tree

90 files changed

+1110
-164
lines changed

Some content is hidden

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

90 files changed

+1110
-164
lines changed

solution/0000-0099/0039.Combination Sum/README_EN.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,25 @@ These are the only two combinations.
4848

4949
## Solutions
5050

51-
DFS.
51+
**Solution 1: Sorting + Pruning + Backtracking (Two Implementations)**
52+
53+
We can first sort the array to facilitate pruning.
54+
55+
Next, we design a function $dfs(i, s)$, which means starting the search from index $i$ with a remaining target value of $s$. Here, $i$ and $s$ are both non-negative integers, the current search path is $t$, and the answer is $ans$.
56+
57+
In the function $dfs(i, s)$, we first check whether $s$ is $0$. If it is, we add the current search path $t$ to the answer $ans$, and then return. If $s \lt candidates[i]$, it means that the elements of the current index and the following indices are all greater than the remaining target value $s$, and the path is invalid, so we return directly. Otherwise, we start the search from index $i$, and the search index range is $j \in [i, n)$, where $n$ is the length of the array $candidates$. During the search, we add the element of the current index to the search path $t$, recursively call the function $dfs(j, s - candidates[j])$, and after the recursion ends, we remove the element of the current index from the search path $t$.
58+
59+
We can also change the implementation logic of the function $dfs(i, s)$ to another form. In the function $dfs(i, s)$, we first check whether $s$ is $0$. If it is, we add the current search path $t$ to the answer $ans$, and then return. If $i \geq n$ or $s \lt candidates[i]$, the path is invalid, so we return directly. Otherwise, we consider two situations, one is not selecting the element of the current index, that is, recursively calling the function $dfs(i + 1, s)$, and the other is selecting the element of the current index, that is, recursively calling the function $dfs(i, s - candidates[i])$.
60+
61+
In the main function, we just need to call the function $dfs(0, target)$ to get the answer.
62+
63+
The time complexity is $O(2^n \times n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $candidates$. Due to pruning, the actual time complexity is much less than $O(2^n \times n)$.
64+
65+
Similar problems:
66+
67+
- [40. Combination Sum II](/solution/0000-0099/0040.Combination%20Sum%20II/README_EN.md)
68+
- [77. Combinations](/solution/0000-0099/0077.Combinations/README_EN.md)
69+
- [216. Combination Sum III](/solution/0200-0299/0216.Combination%20Sum%20III/README_EN.md)
5270

5371
<!-- tabs:start -->
5472

solution/0000-0099/0040.Combination Sum II/README_EN.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,25 @@
4646

4747
## Solutions
4848

49-
DFS.
49+
**Solution 1: Sorting + Pruning + Backtracking (Two Implementations)**
50+
51+
We can first sort the array to facilitate pruning and skipping duplicate numbers.
52+
53+
Next, we design a function $dfs(i, s)$, which means starting the search from index $i$ with a remaining target value of $s$. Here, $i$ and $s$ are both non-negative integers, the current search path is $t$, and the answer is $ans$.
54+
55+
In the function $dfs(i, s)$, we first check whether $s$ is $0$. If it is, we add the current search path $t$ to the answer $ans$, and then return. If $i \geq n$ or $s \lt candidates[i]$, the path is invalid, so we return directly. Otherwise, we start the search from index $i$, and the search index range is $j \in [i, n)$, where $n$ is the length of the array $candidates$. During the search, if $j \gt i$ and $candidates[j] = candidates[j - 1]$, it means that the current number is the same as the previous number, we can skip the current number because the previous number has been searched. Otherwise, we add the current number to the search path $t$, recursively call the function $dfs(j + 1, s - candidates[j])$, and after the recursion ends, we remove the current number from the search path $t$.
56+
57+
We can also change the implementation logic of the function $dfs(i, s)$ to another form. If we choose the current number, we add the current number to the search path $t$, then recursively call the function $dfs(i + 1, s - candidates[i])$, and after the recursion ends, we remove the current number from the search path $t$. If we do not choose the current number, we can skip all numbers that are the same as the current number, then recursively call the function $dfs(j, s)$, where $j$ is the index of the first number that is different from the current number.
58+
59+
In the main function, we just need to call the function $dfs(0, target)$ to get the answer.
60+
61+
The time complexity is $O(2^n \times n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $candidates$. Due to pruning, the actual time complexity is much less than $O(2^n \times n)$.
62+
63+
Similar problems:
64+
65+
- [39. Combination Sum](/solution/0000-0099/0039.Combination%20Sum/README_EN.md)
66+
- [77. Combinations](/solution/0000-0099/0077.Combinations/README_EN.md)
67+
- [216. Combination Sum III](/solution/0200-0299/0216.Combination%20Sum%20III/README_EN.md)
5068

5169
<!-- tabs:start -->
5270

solution/0000-0099/0041.First Missing Positive/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@
4747

4848
**方法一:原地交换**
4949

50-
我们假设数组 `nums` 长度为 $n$,那么最小的正整数一定在 $[1, .., n + 1]$ 之间。我们可以遍历数组,将数组中的每个数 $x$ 交换到它应该在的位置上,即 $x$ 应该在的位置为 $x - 1$。如果 $x$ 不在 $[1, n + 1]$ 之间,那么我们就不用管它。
50+
我们假设数组 $nums$ 长度为 $n$,那么最小的正整数一定在 $[1, .., n + 1]$ 之间。我们可以遍历数组,将数组中的每个数 $x$ 交换到它应该在的位置上,即 $x$ 应该在的位置为 $x - 1$。如果 $x$ 不在 $[1, n + 1]$ 之间,那么我们就不用管它。
5151

5252
遍历结束后,我们再遍历数组,如果 $i+1$ 不等于 $nums[i]$,那么 $i+1$ 就是我们要找的最小的正整数。
5353

54-
时间复杂度 $O(n)$,空间复杂度 $O(1)$。
54+
时间复杂度 $O(n)$,其中 $n$ 是数组的长度。空间复杂度 $O(1)$。
5555

5656
<!-- tabs:start -->
5757

solution/0000-0099/0041.First Missing Positive/README_EN.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@
4343

4444
## Solutions
4545

46+
**Solution 1: In-place Swap**
47+
48+
We assume the length of the array $nums$ is $n$, then the smallest positive integer must be in the range $[1, .., n + 1]$. We can traverse the array and swap each number $x$ to its correct position, that is, the position $x - 1$. If $x$ is not in the range $[1, n + 1]$, then we can ignore it.
49+
50+
After the traversal, we traverse the array again. If $i+1$ is not equal to $nums[i]$, then $i+1$ is the smallest positive integer we are looking for.
51+
52+
The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$.
53+
4654
<!-- tabs:start -->
4755

4856
### **Python3**

solution/0000-0099/0042.Trapping Rain Water/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343

4444
**方法一:动态规划**
4545

46-
我们定义 $left[i]$ 表示下标 $i$ 位置及其左边的最高柱子的高度,定义 $right[i]$ 表示下标 $i$ 位置及其右边的最高柱子的高度。那么下标 $i$ 位置能接的雨水量为 $min(left[i], right[i]) - height[i]$。我们遍历数组,计算出 $left[i]$ 和 $right[i]$,最后答案为 $\sum_{i=0}^{n-1} min(left[i], right[i]) - height[i]$。
46+
我们定义 $left[i]$ 表示下标 $i$ 位置及其左边的最高柱子的高度,定义 $right[i]$ 表示下标 $i$ 位置及其右边的最高柱子的高度。那么下标 $i$ 位置能接的雨水量为 $\min(left[i], right[i]) - height[i]$。我们遍历数组,计算出 $left[i]$ 和 $right[i]$,最后答案为 $\sum_{i=0}^{n-1} min(left[i], right[i]) - height[i]$。
4747

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

solution/0000-0099/0042.Trapping Rain Water/README_EN.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@
3535

3636
**Solution 1: Dynamic Programming**
3737

38-
We define $left[i]$ as the height of the highest pillar to the left of and including the position with index $i$, and define $right[i]$ as the height of the highest pillar to the right of and including the position with index $i$. Then the amount of rain water that can be trapped at the position with index $i$ is $min(left[i], right[i]) - height[i]$. We traverse the array, calculate $left[i]$ and $right[i]$, and the answer is $\sum_{i=0}^{n-1} min(left[i], right[i]) - height[i]$.
38+
We define $left[i]$ as the height of the highest bar to the left of and including the position at index $i$, and $right[i]$ as the height of the highest bar to the right of and including the position at index $i$. Therefore, the amount of rainwater that can be trapped at index $i$ is $min(left[i], right[i]) - height[i]$. We traverse the array to calculate $left[i]$ and $right[i]$, and the final answer is $\sum_{i=0}^{n-1} min(left[i], right[i]) - height[i]$.
3939

40-
The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the array.
40+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array.
4141

4242
<!-- tabs:start -->
4343

solution/0000-0099/0043.Multiply Strings/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,20 @@
4040

4141
**方法一:数学乘法模拟**
4242

43-
假设 `num1``num2` 的长度分别为 $m$ 和 $n$,则它们的乘积的长度最多为 $m + n$。
43+
假设 $num1$$num2$ 的长度分别为 $m$ 和 $n$,则它们的乘积的长度最多为 $m + n$。
4444

4545
证明如下:
4646

47-
- 如果 `num1``num2` 都取最小值,那么它们的乘积为 ${10}^{m - 1} \times {10}^{n - 1} = {10}^{m + n - 2}$,长度为 $m + n - 1$。
48-
- 如果 `num1``num2` 都取最大值,那么它们的乘积为 $({10}^m - 1) \times ({10}^n - 1) = {10}^{m + n} - {10}^m - {10}^n + 1$,长度为 $m + n$。
47+
- 如果 $num1$$num2$ 都取最小值,那么它们的乘积为 ${10}^{m - 1} \times {10}^{n - 1} = {10}^{m + n - 2}$,长度为 $m + n - 1$。
48+
- 如果 $num1$$num2$ 都取最大值,那么它们的乘积为 $({10}^m - 1) \times ({10}^n - 1) = {10}^{m + n} - {10}^m - {10}^n + 1$,长度为 $m + n$。
4949

5050
因此,我们可以申请一个长度为 $m + n$ 的数组,用于存储乘积的每一位。
5151

5252
从低位到高位,依次计算乘积的每一位,最后将数组转换为字符串即可。
5353

5454
注意判断最高位是否为 $0$,如果是,则去掉。
5555

56-
时间复杂度 $O(m \times n)$,空间复杂度 $O(m + n)$。其中 $m$ 和 $n$ 分别为 `num1``num2` 的长度。
56+
时间复杂度 $O(m \times n)$,空间复杂度 $O(m + n)$。其中 $m$ 和 $n$ 分别为 $num1$$num2$ 的长度。
5757

5858
<!-- tabs:start -->
5959

solution/0000-0099/0043.Multiply Strings/README_EN.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,23 @@
2727

2828
## Solutions
2929

30+
**Solution 1: Simulating Mathematical Multiplication**
31+
32+
Assume the lengths of $num1$ and $num2$ are $m$ and $n$ respectively, then the length of their product can be at most $m + n$.
33+
34+
The proof is as follows:
35+
36+
- If $num1$ and $num2$ both take the minimum value, then their product is ${10}^{m - 1} \times {10}^{n - 1} = {10}^{m + n - 2}$, with a length of $m + n - 1$.
37+
- If $num1$ and $num2$ both take the maximum value, then their product is $({10}^m - 1) \times ({10}^n - 1) = {10}^{m + n} - {10}^m - {10}^n + 1$, with a length of $m + n$.
38+
39+
Therefore, we can apply for an array of length $m + n$ to store each digit of the product.
40+
41+
From the least significant digit to the most significant digit, we calculate each digit of the product in turn, and finally convert the array into a string.
42+
43+
Note to check whether the most significant digit is $0$, if it is, remove it.
44+
45+
The time complexity is $O(m \times n)$, and the space complexity is $O(m + n)$. Here, $m$ and $n$ are the lengths of $num1$ and $num2$ respectively.
46+
3047
<!-- tabs:start -->
3148

3249
### **Python3**

solution/0000-0099/0044.Wildcard Matching/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060

6161
**方法一:动态规划**
6262

63-
定义状态 $dp[i][j]$ 表示 $s$ 的前 $i$ 个字符和 $p$ 的前 $j$ 个字符是否匹配。
63+
我们定义状态 $dp[i][j]$ 表示 $s$ 的前 $i$ 个字符和 $p$ 的前 $j$ 个字符是否匹配。
6464

6565
状态转移方程如下:
6666

@@ -73,7 +73,7 @@ dp[i-1][j-1] \lor dp[i-1][j] \lor dp[i][j-1] & \text{if } p[j-1]=\text{*} \\
7373
\end{cases}
7474
$$
7575

76-
时间复杂度 $O(m\times n)$,空间复杂度 $O(m\times n)$。
76+
时间复杂度 $O(m \times n)$,空间复杂度 $O(m \times n)$。其中 $m$ 和 $n$ 分别为 $s$ 和 $p$ 的长度
7777

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

solution/0000-0099/0044.Wildcard Matching/README_EN.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,23 @@
4949

5050
## Solutions
5151

52+
**Solution 1: Dynamic Programming**
53+
54+
We define the state $dp[i][j]$ to represent whether the first $i$ characters of $s$ match the first $j$ characters of $p$.
55+
56+
The state transition equation is as follows:
57+
58+
$$
59+
dp[i][j]=
60+
\begin{cases}
61+
dp[i-1][j-1] & \text{if } s[i-1]=p[j-1] \text{ or } p[j-1]=\text{?} \\
62+
dp[i-1][j-1] \lor dp[i-1][j] \lor dp[i][j-1] & \text{if } p[j-1]=\text{*} \\
63+
\text{false} & \text{otherwise}
64+
\end{cases}
65+
$$
66+
67+
The time complexity is $O(m \times n)$, and the space complexity is $O(m \times n)$. Here, $m$ and $n$ are the lengths of $s$ and $p$ respectively.
68+
5269
<!-- tabs:start -->
5370

5471
### **Python3**

0 commit comments

Comments
 (0)