Skip to content

Commit e173ed8

Browse files
committed
feat: add solutions to lc problems: No.0001~0007
1 parent c61860e commit e173ed8

File tree

8 files changed

+89
-7
lines changed

8 files changed

+89
-7
lines changed

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@
5757

5858
**方法一:哈希表**
5959

60-
我们可以用哈希表(字典) $m$ 存放数组值以及对应的下标。
60+
我们可以用哈希表 $m$ 存放数组值以及对应的下标。
6161

62-
遍历数组 `nums`,当发现 `target - nums[i]` 在哈希表中,说明找到了目标值,返回 `target - nums[i]` 的下标和 `i` 即可。
62+
遍历数组 `nums`,当发现 `target - nums[i]` 在哈希表中,说明找到了目标值,返回 `target - nums[i]` 的下标以及 $i$ 即可。
6363

6464
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 `nums` 的长度。
6565

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

+8
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@
4848

4949
## Solutions
5050

51+
**Approach 1: Hash Table**
52+
53+
We can use the hash table $m$ to store the array value and the corresponding subscript.
54+
55+
Traverse the array `nums`, when you find `target - nums[i]` in the hash table, it means that the target value is found, and the index of `target - nums[i]` and $i$ are returned.
56+
57+
The time complexity is $O(n)$ and the space complexity is $O(n)$. Where $n$ is the length of the array `nums`.
58+
5159
<!-- tabs:start -->
5260

5361
### **Python3**

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,13 @@
5252

5353
**方法一:模拟**
5454

55-
同时遍历两个链表 `l1`, `l2`,对应节点值相加,进位记为 `carry`。当 `l1`, `l2` 同时遍历结束,并且 `carry``0` 时,结束遍历
55+
我们同时遍历两个链表 $l_1$ 和 $l_2$,并使用变量 $carry$ 表示当前是否有进位
5656

57-
时间复杂度 $O(\max(m, n))$,其中 $m$, $n$ 分别表示两个链表的长度。忽略结果链表的空间消耗,空间复杂度 $O(1)$。
57+
每次遍历时,我们取出对应链表的当前位,计算它们与进位 $carry$ 的和,然后更新进位的值,最后将当前位的值加入答案链表。如果两个链表都遍历完了,并且进位为 $0$ 时,遍历结束。
58+
59+
最后我们返回答案链表的头节点即可。
60+
61+
时间复杂度 $O(max(m, n))$,其中 $m$ 和 $n$ 分别为两个链表的长度。我们需要遍历两个链表的全部位置,而处理每个位置只需要 $O(1)$ 的时间。忽略答案的空间消耗,空间复杂度 $O(1)$。
5862

5963
<!-- tabs:start -->
6064

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

+10
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,16 @@
4242

4343
## Solutions
4444

45+
**Approach 1: Simulation**
46+
47+
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.
48+
49+
Each time we traverse, we take out the current bit of the corresponding linked list, calculate the sum with the carry $carry$, and then update the value of the carry. Then we add the current bit to the answer linked list. If both linked lists are traversed, and the carry is $0$, the traversal ends.
50+
51+
Finally, we return the head node of the answer linked list.
52+
53+
The time complexity is $O(max (m, n))$, where $m$ and $n$ are the lengths of the two linked lists. We need to traverse the entire position of the two linked lists, and each position only needs $O(1)$ time. Ignoring the space consumption of the answer, the space complexity is $O(1)$.
54+
4555
<!-- tabs:start -->
4656

4757
### **Python3**

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

+21
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,27 @@ Notice that the answer must be a substring, &quot;pwke&quot; is a subsequence an
4242

4343
## Solutions
4444

45+
**Approach 1: Two pointers + Hash Table**
46+
47+
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`.
48+
49+
For each character $s[j]$ in the string `s`, we call it $c$. If $c$ exists in the window $s[i..j-1]$, we move $i$ to the right until $s[i..j-1]$ does not contain `c`. Then we add `c` to the hash table. At this time, the window $s[i..j]$ does not contain repeated elements, and we update the maximum value of `ans`.
50+
51+
Finally, return `ans`.
52+
53+
The time complexity is $O(n)$, where $n$ represents the length of the string `s`.
54+
55+
Two pointers algorithm template:
56+
57+
```java
58+
for (int i = 0, j = 0; i < n; ++i) {
59+
while (j < i && check(j, i)) {
60+
++j;
61+
}
62+
// logic of specific problem
63+
}
64+
```
65+
4566
<!-- tabs:start -->
4667

4768
### **Python3**

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

+14-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,20 @@
3232

3333
## Solutions
3434

35-
Dynamic programming.
35+
**Approach 1: Dynamic Programming**
36+
37+
Set $dp[i][j]$ to indicate whether the string $s[i..j]$ is a palindrome.
38+
39+
- When $j - i \lt 2$, that is, the string length is `2`, as long as $s[i] == s[j]$, then $dp[i][j]$ is `true`.
40+
- When $j - i \ge 2$, there is $dp[i][j] = dp[i + 1][j - 1] \cap s[i] == s[j]$.
41+
42+
The time complexity is $O(n^2)$ and the space complexity is $O(n^2)$. Where $n$ is the length of the string $s$.
43+
44+
**Approach 2: Enumerate the Palindrome Center**
45+
46+
We can enumerate the palindrome center, spread to both sides, and find the longest palindrome.
47+
48+
The time complexity is $O(n^2)$ and the space complexity is $O(1)$. Where $n$ is the length of the string $s$.
3649

3750
<!-- tabs:start -->
3851

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

+8
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@ P I
5858

5959
## Solutions
6060

61+
**Approach 1: Simulation**
62+
63+
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.
64+
65+
We traverse the string $s$ from left to right. Each time we traverse a character $c$, we append it to $g[i]$. If at this time $i=0$ or $i=numRows-1$, it means that the current character is at the turning point of the $Z$-shaped arrangement. We reverse the value of $k$, that is, $k=-k$. Next, we update the value of $i$ to $i+k$, that is, move up or down one row. Continue to traverse the next character until the string $s$ is traversed. We return the string that all rows in $g$ are concatenated.
66+
67+
Time complexity $O(n)$, space complexity $O(n)$, where $n$ is the length of the string $s$.
68+
6169
<!-- tabs:start -->
6270

6371
### **Python3**

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

+20-2
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,27 @@
3939

4040
## Solutions
4141

42-
**Approach 1: Pop and Push Digits & Check before Overflow**
42+
**Approach 1: Mathematical**
4343

44-
Time complexity $O(log|x|)$, Space complexity $O(1)$.
44+
Let $mi$ and $mx$ be $-2^{31}$ and $2^{31} - 1$ respectively. The reversed result $ans$ needs to satisfy $mi \le ans \le mx$.
45+
46+
We can get the last digit $y$ of $x$ by repeatedly taking the remainder of $x$ by $10$, and then add $y$ to the end of $ans$. Before adding $y$, we need to determine whether $ans$ will overflow. That is, whether $ans \times 10 + y$ is within the range $[mi, mx]$.
47+
48+
If $x \gt 0$, then $ans \times 10 + y \leq mx$ should be satisfied. That is, $ans \times 10 + y \leq \left \lfloor \frac{mx}{10} \right \rfloor \times 10 + 7$. Rearrange to get $(ans - \left \lfloor \frac{mx}{10} \right \rfloor) \times 10 \leq 7 - y$.
49+
50+
The following conditions need to be satisfied for the above inequality to hold:
51+
52+
- When $ans \lt \left \lfloor \frac{mx}{10} \right \rfloor$, the inequality obviously holds;
53+
- When $ans = \left \lfloor \frac{mx}{10} \right \rfloor$, the necessary and sufficient conditions for the above inequality to hold are $y \leq 7$. If $ans = \left \lfloor \frac{mx}{10} \right \rfloor$ and can still continue to add digits, it means that the current digit is the most significant digit. Therefore, $y$ must be less than or equal to $2$, so the inequality must hold;
54+
- When $ans \gt \left \lfloor \frac{mx}{10} \right \rfloor$, the inequality obviously does not hold.
55+
56+
Therefore, when $x \gt 0$, the necessary and sufficient conditions for the inequality to hold are $ans \leq \left \lfloor \frac{mx}{10} \right \rfloor$.
57+
58+
Similarly, when $x \lt 0$, the necessary and sufficient conditions for the inequality to hold are $ans \geq \left \lfloor \frac{mi}{10} \right \rfloor$.
59+
60+
Therefore, we can determine whether $ans$ will overflow by determining whether $ans$ is within the range $[\left \lfloor \frac{mi}{10} \right \rfloor, \left \lfloor \frac{mx}{10} \right \rfloor]$. If so, return $0$. Otherwise, add $y$ to the end of $ans$, and then remove the last digit of $x$, that is, $x \gets \left \lfloor \frac{x}{10} \right \rfloor$.
61+
62+
Time complexity $O(\log |x|)$, where $|x|$ is the absolute value of $x$. Space complexity $O(1)$.
4563

4664
<!-- tabs:start -->
4765

0 commit comments

Comments
 (0)