Skip to content

Commit 65c5ade

Browse files
authored
chore: update lc problems (doocs#1095)
1 parent ff5cc9a commit 65c5ade

File tree

118 files changed

+356
-334
lines changed

Some content is hidden

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

118 files changed

+356
-334
lines changed

solution/0000-0099/0001.Two Sum/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848

4949
## Solutions
5050

51-
**Approach 1: Hash Table**
51+
**Solution 1: Hash Table**
5252

5353
We can use the hash table $m$ to store the array value and the corresponding subscript.
5454

solution/0000-0099/0002.Add Two Numbers/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242

4343
## Solutions
4444

45-
**Approach 1: Simulation**
45+
**Solution 1: Simulation**
4646

4747
We traverse two linked lists $l_1$ and $l_2$ at the same time, and use the variable $carry$ to indicate whether there is a carry.
4848

solution/0000-0099/0003.Longest Substring Without Repeating Characters/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Notice that the answer must be a substring, "pwke" is a subsequence an
4242

4343
## Solutions
4444

45-
**Approach 1: Two pointers + Hash Table**
45+
**Solution 1: Two pointers + Hash Table**
4646

4747
Define a hash table to record the characters in the current window. Let $i$ and $j$ represent the start and end positions of the non-repeating substring, respectively. The length of the longest non-repeating substring is recorded by `ans`.
4848

solution/0000-0099/0005.Longest Palindromic Substring/README_EN.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
## Solutions
3434

35-
**Approach 1: Dynamic Programming**
35+
**Solution 1: Dynamic Programming**
3636

3737
Set $dp[i][j]$ to indicate whether the string $s[i..j]$ is a palindrome.
3838

@@ -41,7 +41,7 @@ Set $dp[i][j]$ to indicate whether the string $s[i..j]$ is a palindrome.
4141

4242
The time complexity is $O(n^2)$ and the space complexity is $O(n^2)$. Where $n$ is the length of the string $s$.
4343

44-
**Approach 2: Enumerate the Palindrome Center**
44+
**Solution 2: Enumerate the Palindrome Center**
4545

4646
We can enumerate the palindrome center, spread to both sides, and find the longest palindrome.
4747

solution/0000-0099/0006.Zigzag Conversion/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ P I
5858

5959
## Solutions
6060

61-
**Approach 1: Simulation**
61+
**Solution 1: Simulation**
6262

6363
We use a 2D array $g$ to simulate the process of the $Z$-shaped arrangement, where $g[i][j]$ represents the character in the $i$th row and the $j$th column. Initially, $i=0$, and we also define a direction variable $k$, initially $k=-1$, which means up.
6464

solution/0000-0099/0007.Reverse Integer/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939

4040
## Solutions
4141

42-
**Approach 1: Mathematical**
42+
**Solution 1: Mathematical**
4343

4444
Let $mi$ and $mx$ be $-2^{31}$ and $2^{31} - 1$ respectively. The reversed result $ans$ needs to satisfy $mi \le ans \le mx$.
4545

solution/0000-0099/0012.Integer to Roman/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ M 1000</pre>
6262

6363
## Solutions
6464

65-
**Approach 1: Greedy**
65+
**Solution 1: Greedy**
6666

6767
We can list all possible symbols $cs$ and corresponding values $vs$ first, then enumerate the value $vs[i]$ from large to small, and use the symbol $cs[i]$ as much as possible each time until the number $num$ becomes $0$.
6868

solution/0000-0099/0013.Roman to Integer/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ M 1000</pre>
6464

6565
## Solutions
6666

67-
**Approach 1: Hash table + simulation**
67+
**Solution 1: Hash table + simulation**
6868

6969
We first use a hash table $d$ to record the numerical value corresponding to each character, and then traverse the string $s$ from left to right. If the numerical value corresponding to the current character is less than the numerical value corresponding to the right character, subtract the numerical value corresponding to the current character, otherwise add the numerical value corresponding to the current character.
7070

solution/0000-0099/0014.Longest Common Prefix/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535

3636
## Solutions
3737

38-
**Approach 1: Character Comparison**
38+
**Solution 1: Character Comparison**
3939

4040
We take the first string $strs[0]$ as the benchmark, and compare the $i$th character of the string after it with the $i$th character of $strs[0]$. If it is the same, continue to compare the next character, otherwise return the first $i$ characters of $strs[0]$.
4141

solution/0000-0099/0015.3Sum/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ Notice that the order of the output and the order of the triplets does not matte
4848

4949
## Solutions
5050

51-
**Approach 1: Sort + Two Pointers**
51+
**Solution 1: Sort + Two Pointers**
5252

5353
We notice that the problem does not require us to return the triplet in order, so we might as well sort the array first, which makes it easy to skip duplicate elements.
5454

solution/0000-0099/0018.4Sum/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040

4141
## Solutions
4242

43-
**Approach 1: Two Pointers**
43+
**Solution 1: Two Pointers**
4444

4545
Time complexity $O(n^3)$, Space complexity $O(\log n)$.
4646

solution/0000-0099/0024.Swap Nodes in Pairs/README_EN.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@
3838

3939
## Solutions
4040

41-
**Approach 1: Iteration**
41+
**Solution 1: Iteration**
4242

4343
Time complexity $O(n)$, Space complexity $O(1)$.
4444

45-
**Approach 2: Recursion**
45+
**Solution 2: Recursion**
4646

4747
Time complexity $O(n)$, Space complexity $O(n)$.
4848

solution/0000-0099/0025.Reverse Nodes in k-Group/README_EN.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@
3939

4040
## Solutions
4141

42-
**Approach 1: Iteration**
42+
**Solution 1: Iteration**
4343

4444
Time complexity $O(n)$, Space complexity $O(1)$.
4545

46-
**Approach 2: Recursion**
46+
**Solution 2: Recursion**
4747

4848
Time complexity $O(n)$, Space complexity $O(\log _k n)$.
4949

solution/0000-0099/0027.Remove Element/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ It does not matter what you leave beyond the returned k (hence they are undersco
6565

6666
## Solutions
6767

68-
**Approach 1: One Pass**
68+
**Solution 1: One Pass**
6969

7070
We use the variable $k$ to record the number of elements that are not equal to $val$.
7171

solution/0000-0099/0028.Find the Index of the First Occurrence in a String/README_EN.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@ The first occurrence is at index 0, so we return 0.
3434

3535
## Solutions
3636

37-
**Approach 1: Traverse**
37+
**Solution 1: Traverse**
3838

3939
Time complexity $O((n-m) \times m)$, Space complexity $O(1)$.
4040

41-
**Approach 2: Rabin-Karp**
41+
**Solution 2: Rabin-Karp**
4242

4343
Time complexity $O(n+m)$, Space complexity $O(1)$.
4444

45-
**Approach 3: KMP**
45+
**Solution 3: KMP**
4646

4747
Time complexity $O(n+m)$, Space complexity $O(m)$.
4848

solution/0000-0099/0029.Divide Two Integers/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939

4040
## Solutions
4141

42-
**Approach 1: Quick Power**
42+
**Solution 1: Quick Power**
4343

4444
Time complexity $O(\log a \times \log b)$, Space complexity $O(1)$.
4545

solution/0000-0099/0030.Substring with Concatenation of All Words/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ The substring starting at 12 is &quot;thefoobar&quot;. It is the concatenation o
5959

6060
## Solutions
6161

62-
**Approach 1: Hash Table + Sliding Window**
62+
**Solution 1: Hash Table + Sliding Window**
6363

6464
We use a hash table $cnt$ to count the number of times each word in $words$ appears, and use a hash table $cnt1$ to count the number of times each word in the current sliding window appears. Let the length of the string $s$ be $m$, the number of words in the string array $words$ be $n$, and the length of each word be $k$.
6565

solution/0000-0099/0031.Next Permutation/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454

5555
## Solutions
5656

57-
**Approach 1: Two traversals**
57+
**Solution 1: Two traversals**
5858

5959
We first traverse the array from back to front and find the first position $i$ where $nums[i] \lt nums[i + 1]$.
6060

solution/0000-0099/0032.Longest Valid Parentheses/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040

4141
## Solutions
4242

43-
**Approach 1: Dynamic Programming**
43+
**Solution 1: Dynamic Programming**
4444

4545
We define $f[i]$ to be the length of the longest valid parentheses that ends with $s[i-1]$, and the answer is $max(f[i])$.
4646

solution/0000-0099/0036.Valid Sudoku/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464

6565
## Solutions
6666

67-
**Approach 1: Traversal once**
67+
**Solution 1: Traversal once**
6868

6969
The valid sudoku satisfies the following three conditions:
7070

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
## Solutions
3535

36-
**Approach 1: Dynamic Programming**
36+
**Solution 1: Dynamic Programming**
3737

3838
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]$.
3939

solution/0000-0099/0045.Jump Game II/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242

4343
## Solutions
4444

45-
**Approach 1: Greedy**
45+
**Solution 1: Greedy**
4646

4747
We can use a variable $mx$ to record the furthest position that can be reached at the current position, and use a variable $last$ to record the last jump position, and use a variable $ans$ to record the number of jumps.
4848

solution/0000-0099/0048.Rotate Image/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434

3535
## Solutions
3636

37-
**Approach 1: In-place**
37+
**Solution 1: In-place**
3838

3939
According to the requirements of the problem, we actually need to rotate $matrix[i][j]$ to $matrix[j][n - i - 1]$.
4040

solution/0000-0099/0054.Spiral Matrix/README_EN.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@
3333

3434
## Solutions
3535

36-
**Approach 1: Simulation**
36+
**Solution 1: Simulation**
3737

3838
We use $i$ and $j$ to respectively represent the row and column of the current element being visited, and use $k$ to represent the current direction. We use an array or hash table $vis$ to record whether each element has been visited. After each element is visited, it is marked as visited, and then the current direction is moved forward one step. If the forward step goes out of bounds or has been visited, the direction is changed and continued. Move forward until the entire matrix is traversed.
3939

4040
The time complexity is $O(m \times n)$ and the space complexity is $O(m \times n)$. Where $m$ and $n$ are the number of rows and columns of the matrix.
4141

4242
For the visited elements, we can also add a constant $300$ to their values, so we do not need an extra $vis$ array or hash table to record whether it has been visited, thus reducing the space complexity to $O(1)$.
4343

44-
**Approach 2: Layer-by-layer Simulation**
44+
**Solution 2: Layer-by-layer Simulation**
4545

4646
We can also traverse and store the matrix elements from the outside to the inside layer by layer.
4747

solution/0000-0099/0055.Jump Game/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535

3636
## Solutions
3737

38-
**Approach 1: Greedy**
38+
**Solution 1: Greedy**
3939

4040
We use a variable $mx$ to maintain the farthest index that can be reached, initially $mx = 0$.
4141

solution/0000-0099/0058.Length of Last Word/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444

4545
## Solutions
4646

47-
**Approach 1: Reverse traversal + two pointers**
47+
**Solution 1: Reverse traversal + two pointers**
4848

4949
We start traversing from the end of the string $s$, finding the last character of the last word, which is not a space, and the subscript is $i$. Then continue to traverse forward to find the first space character, which is the character before the first character of the last word, and mark it as $j$. Then the length of the last word is $i - j$.
5050

solution/0000-0099/0071.Simplify Path/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555

5656
## Solutions
5757

58-
**Approach 1: Stack**
58+
**Solution 1: Stack**
5959

6060
We first split the path into a number of substrings split by `'/'`. Then, we traverse each substring and perform the following operations based on the content of the substring:
6161

solution/0000-0099/0073.Set Matrix Zeroes/README_EN.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,15 @@
4444

4545
## Solutions
4646

47-
**Approach 1: Array Mark**
47+
**Solution 1: Array Mark**
4848

4949
We use arrays `rows` and `cols` to mark the rows and columns to be cleared.
5050

5151
Then traverse the matrix again, and clear the elements in the rows and columns marked in `rows` and `cols`.
5252

5353
The time complexity is $O(m\times n)$, and the space complexity is $O(m+n)$. Where $m$ and $n$ are the number of rows and columns of the matrix respectively.
5454

55-
**Approach 2: Mark in Place**
55+
**Solution 2: Mark in Place**
5656

5757
In the first method, we use an additional array to mark the rows and columns to be cleared. In fact, we can also use the first row and first column of the matrix to mark them, without creating an additional array.
5858

solution/0000-0099/0076.Minimum Window Substring/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ Since the largest window of s only has one &#39;a&#39;, return empty string.
4949

5050
## Solutions
5151

52-
**Approach 1: Counting + Two Pointers**
52+
**Solution 1: Counting + Two Pointers**
5353

5454
We use a hash table or array $need$ to count the number of characters in string $t$, and use another hash table or array $window$ to count the number of characters in the sliding window. In addition, define two pointers $j$ and $i$ pointing to the left and right boundaries of the window, and the variable $cnt$ represents the number of characters in $t$ that are already included in the window. The variables $k$ and $mi$ represent the starting position and length of the minimum cover substring respectively.
5555

solution/0000-0099/0088.Merge Sorted Array/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ Note that because m = 0, there are no elements in nums1. The 0 is only there to
5555

5656
## Solutions
5757

58-
**Approach 1: Two Pointers**
58+
**Solution 1: Two Pointers**
5959

6060
We use two pointers $i$ and $j$ pointing to the end of two arrays, and a pointer $k$ pointing to the end of the merged array.
6161

solution/0100-0199/0121.Best Time to Buy and Sell Stock/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Note that buying on day 2 and selling on day 1 is not allowed because you must b
3838

3939
## Solutions
4040

41-
**Approach 1: Enumerate + Maintain the Minimum Value of the Prefix**
41+
**Solution 1: Enumerate + Maintain the Minimum Value of the Prefix**
4242

4343
We can enumerate each element of the array $nums$ as the selling price. Then we need to find a minimum value in front of it as the purchase price to maximize the profit.
4444

solution/0100-0199/0122.Best Time to Buy and Sell Stock II/README_EN.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ Total profit is 4.
4848

4949
## Solutions
5050

51-
**Approach 1: Greedy**
51+
**Solution 1: Greedy**
5252

5353
From the second day, if the stock price on that day is greater than the previous day, buy it on the previous day and sell it on that day to get a profit. If the stock price on that day is less than the previous day, do not buy it or sell it. That is to say, all the rising trading days are bought and sold, and all the falling trading days are not bought or sold, and the final profit is the maximum.
5454

5555
The time complexity is $O(n)$, where $n$ is the length of the array `prices`. The space complexity is $O(1)$.
5656

57-
**Approach 2: Dynamic Programming**
57+
**Solution 2: Dynamic Programming**
5858

5959
Let $f[i][j]$ represent the maximum profit after the $i$th day of trading, where $j$ represents whether the current stock is held, and $j=0$ when the stock is held, and $j=1$ when the stock is not held. The initial state is $f[0][0]=-prices[0]$, and all other states are $0$.
6060

@@ -75,9 +75,9 @@ The final answer is $f[n-1][1]$, where $n$ is the length of the array `prices`.
7575

7676
The time complexity is $O(n)$ and the space complexity is $O(n)$. $n$ is the length of the array `prices`.
7777

78-
**Approach 3: Dynamic Programming (space optimization)**
78+
**Solution 3: Dynamic Programming (space optimization)**
7979

80-
We can find that in approach 2, the state of the $i$th day only depends on the state of the $i-1$th day, so we can only use two variables to maintain the state of the $i-1$th day. Therefore, the space complexity can be optimized to $O(1)$.
80+
We can find that in Solution 2, the state of the $i$th day only depends on the state of the $i-1$th day, so we can only use two variables to maintain the state of the $i-1$th day. Therefore, the space complexity can be optimized to $O(1)$.
8181

8282
Time complexity $O(n)$, where $n$ is the length of the array `prices`. Space complexity $O(1)$.
8383

solution/0100-0199/0128.Longest Consecutive Sequence/README_EN.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434

3535
## Solutions
3636

37-
**Approach 1: Sorting**
37+
**Solution 1: Sorting**
3838

3939
First, we sort the array, and use a variable $t$ to record the length of the current continuous sequence, and use a variable $ans$ to record the length of the longest continuous sequence.
4040

@@ -48,7 +48,7 @@ Finally, we return the answer $ans$.
4848

4949
Time complexity $O(n \times \log n)$, space complexity $O(\log n)$, where $n$ is the length of the array.
5050

51-
**Approach 2: Hash Table**
51+
**Solution 2: Hash Table**
5252

5353
We use a hash table to store all the elements in the array, and then we traverse the array to find the longest continuous sequence for each element $x$. If the predecessor of the current element $x-1$ is not in the hash table, we use the current element as the starting point, and keep trying to match $x+1, x+2, x+3, \dots$ until we cannot match, the matching length at this time is the length of the longest continuous sequence starting from $x$, so we update the answer.
5454

solution/0100-0199/0135.Candy/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ The third child gets 1 candy because it satisfies the above two conditions.
4444

4545
## Solutions
4646

47-
**Approach 1: Two traversals**
47+
**Solution 1: Two traversals**
4848

4949
We initialize two arrays $left$ and $right$, where $left[i]$ represents the minimum number of candies the current child should get when the current child's score is higher than the left child's score, and $right[i]$ represents the minimum number of candies the current child should get when the current child's score is higher than the right child's score. Initially, $left[i]=1$, $right[i]=1$.
5050

solution/0100-0199/0151.Reverse Words in a String/README_EN.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,13 @@
5050

5151
## Solutions
5252

53-
**Approach 1: Use Language Built-in Functions**
53+
**Solution 1: Use Language Built-in Functions**
5454

5555
We split the string into a list of strings by spaces, then reverse the list, and finally join the list into a string separated by spaces.
5656

5757
Time complexity $O(n)$, space complexity $O(n)$, where $n$ is the length of the string.
5858

59-
**Approach 2: Two Pointers**
59+
**Solution 2: Two Pointers**
6060

6161
We can use two pointers $i$ and $j$, each time we find a word, add it to the result list, then reverse the result list, and finally join the list into a string.
6262

0 commit comments

Comments
 (0)