Skip to content

Commit 0e52e8c

Browse files
committed
feat: add solutions to lc problems
No.0021,0027,0031,0032,0036
1 parent e173ed8 commit 0e52e8c

File tree

7 files changed

+72
-18
lines changed

7 files changed

+72
-18
lines changed

solution/0000-0099/0021.Merge Two Sorted Lists/README.md

+14-9
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,22 @@
4747

4848
**方法一:递归**
4949

50+
我们先判断链表 $l_1$ 和 $l_2$ 是否为空,若其中一个为空,则返回另一个链表。否则,我们比较 $l_1$ 和 $l_2$ 的头节点:
51+
52+
- 若 $l_1$ 的头节点的值小于等于 $l_2$ 的头节点的值,则递归调用函数 $mergeTwoLists(l_1.next, l_2)$,并将 $l_1$ 的头节点与返回的链表头节点相连,返回 $l_1$ 的头节点。
53+
- 否则,递归调用函数 $mergeTwoLists(l_1, l_2.next)$,并将 $l_2$ 的头节点与返回的链表头节点相连,返回 $l_2$ 的头节点。
54+
55+
时间复杂度 $O(m + n)$,空间复杂度 $O(m + n)$。其中 $m$ 和 $n$ 分别为两个链表的长度。
56+
5057
**方法二:迭代**
5158

52-
迭代遍历两链表,比较节点值 val 的大小,进行节点串联,得到最终链表。
59+
我们也可以用迭代的方式来实现两个排序链表的合并。
60+
61+
我们先定义一个虚拟头节点 $dummy$,然后循环遍历两个链表,比较两个链表的头节点,将较小的节点添加到 $dummy$ 的末尾,直到其中一个链表为空,然后将另一个链表的剩余部分添加到 $dummy$ 的末尾。
62+
63+
最后返回 $dummy.next$ 即可。
64+
65+
时间复杂度 $O(m + n)$,其中 $m$ 和 $n$ 分别为两个链表的长度。忽略答案链表的空间消耗,空间复杂度 $O(1)$。
5366

5467
<!-- tabs:start -->
5568

@@ -414,8 +427,6 @@ public class Solution {
414427

415428
### **TypeScript**
416429

417-
递归:
418-
419430
```ts
420431
/**
421432
* Definition for singly-linked list.
@@ -446,8 +457,6 @@ function mergeTwoLists(
446457
}
447458
```
448459

449-
循环:
450-
451460
```ts
452461
/**
453462
* Definition for singly-linked list.
@@ -484,8 +493,6 @@ function mergeTwoLists(
484493

485494
### **Rust**
486495

487-
递归:
488-
489496
```rust
490497
// Definition for singly-linked list.
491498
// #[derive(PartialEq, Eq, Clone, Debug)]
@@ -526,8 +533,6 @@ impl Solution {
526533
}
527534
```
528535

529-
循环:
530-
531536
```rust
532537
// Definition for singly-linked list.
533538
// #[derive(PartialEq, Eq, Clone, Debug)]

solution/0000-0099/0021.Merge Two Sorted Lists/README_EN.md

-8
Original file line numberDiff line numberDiff line change
@@ -403,8 +403,6 @@ public class Solution {
403403

404404
### **TypeScript**
405405

406-
Recursion:
407-
408406
```ts
409407
/**
410408
* Definition for singly-linked list.
@@ -435,8 +433,6 @@ function mergeTwoLists(
435433
}
436434
```
437435

438-
Loop:
439-
440436
```ts
441437
/**
442438
* Definition for singly-linked list.
@@ -473,8 +469,6 @@ function mergeTwoLists(
473469

474470
### **Rust**
475471

476-
Recursion:
477-
478472
```rust
479473
// Definition for singly-linked list.
480474
// #[derive(PartialEq, Eq, Clone, Debug)]
@@ -515,8 +509,6 @@ impl Solution {
515509
}
516510
```
517511

518-
Loop:
519-
520512
```rust
521513
// Definition for singly-linked list.
522514
// #[derive(PartialEq, Eq, Clone, Debug)]

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

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

6666
## Solutions
6767

68+
**Approach 1: One Pass**
69+
70+
We use the variable $k$ to record the number of elements that are not equal to $val$.
71+
72+
Traverse the array $nums$, if the current element $x$ is not equal to $val$, then assign $x$ to $nums[k]$, and increment $k$ by $1$.
73+
74+
Finally, return $k$.
75+
76+
The time complexity is $O(n)$ and the space complexity is $O(1)$, where $n$ is the length of the array $nums$.
77+
6878
<!-- tabs:start -->
6979

7080
### **Python3**

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262

6363
**方法一:两次遍历**
6464

65-
从后往前遍历数组,找到第一个下降的位置 $i$,即 $nums[i] \lt nums[i + 1]$。
65+
我们先从后往前遍历数组,找到第一个下降的位置 $i$,即 $nums[i] \lt nums[i + 1]$。
6666

6767
然后从后往前遍历数组,找到第一个大于 $nums[i]$ 的位置 $j$,即 $nums[j] \gt nums[i]$。交换 $nums[i]$ 和 $nums[j]$,然后将 $nums[i + 1]$ 到 $nums[n - 1]$ 的元素反转,即可得到下一个排列。
6868

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

+8
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@
5454

5555
## Solutions
5656

57+
**Approach 1: Two traversals**
58+
59+
We first traverse the array from back to front and find the first position $i$ where $nums[i] \lt nums[i + 1]$.
60+
61+
Then traverse the array from back to front again and find the first position $j$ where $nums[j] \gt nums[i]$. Swap $nums[i]$ and $nums[j]$, and then reverse the elements from $nums[i + 1]$ to $nums[n - 1]$, the next permutation can be obtained.
62+
63+
The time complexity is $O(n)$ and the space complexity is $O(1)$. Where $n$ is the length of the array.
64+
5765
<!-- tabs:start -->
5866

5967
### **Python3**

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

+27
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,33 @@
4040

4141
## Solutions
4242

43+
**Approach 1: Dynamic Programming**
44+
45+
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])$.
46+
47+
When $i \lt 2$, the length of the string is less than $2$, and there is no valid parentheses, so $f[i] = 0$.
48+
49+
When $i \ge 2$, we consider the length of the longest valid parentheses that ends with $s[i-1]$, that is, $f[i]$:
50+
51+
- If $s[i-1]$ is a left parenthesis, then the length of the longest valid parentheses that ends with $s[i-1]$ must be $0$, so $f[i] = 0$.
52+
- If $s[i-1]$ is a right parenthesis, there are the following two cases:
53+
- If $s[i-2]$ is a left parenthesis, then the length of the longest valid parentheses that ends with $s[i-1]$ is $f[i-2] + 2$.
54+
- If $s[i-2]$ is a right parenthesis, then the length of the longest valid parentheses that ends with $s[i-1]$ is $f[i-1] + 2$, but we also need to consider whether $s[i-f[i-1]-2]$ is a left parenthesis. If it is a left parenthesis, then the length of the longest valid parentheses that ends with $s[i-1]$ is $f[i-1] + 2 + f[i-f[i-1]-2]$.
55+
56+
Therefore, we can get the state transition equation:
57+
58+
$$
59+
\begin{cases}
60+
f[i] = 0, & \text{if } s[i-1] = '(',\\
61+
f[i] = f[i-2] + 2, & \text{if } s[i-1] = ')' \text{ and } s[i-2] = '(',\\
62+
f[i] = f[i-1] + 2 + f[i-f[i-1]-2], & \text{if } s[i-1] = ')' \text{ and } s[i-2] = ')' \text{ and } s[i-f[i-1]-2] = '(',\\
63+
\end{cases}
64+
$$
65+
66+
Finally, we only need to return $max(f)$.
67+
68+
The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the string.
69+
4370
<!-- tabs:start -->
4471

4572
### **Python3**

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

+12
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,18 @@
6464

6565
## Solutions
6666

67+
**Approach 1: Traversal once**
68+
69+
The valid sudoku satisfies the following three conditions:
70+
71+
- The digits are not repeated in each row;
72+
- The digits are not repeated in each column;
73+
- The digits are not repeated in each $3 \times 3$ box.
74+
75+
Traverse the sudoku, for each digit, check whether the row, column and $3 \times 3$ box it is in have appeared the digit. If it is, return `false`. If the traversal is over, return `true`.
76+
77+
The time complexity is $O(C)$ and the space complexity is $O(C)$, where $C$ is the number of empty spaces in the sudoku. In this question, $C=81$.
78+
6779
<!-- tabs:start -->
6880

6981
### **Python3**

0 commit comments

Comments
 (0)