Skip to content

Commit 1b46b27

Browse files
committed
feat: add solutions to lc problem
1 parent d822759 commit 1b46b27

File tree

7 files changed

+60
-120
lines changed

7 files changed

+60
-120
lines changed

solution/0200-0299/0208.Implement Trie (Prefix Tree)/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ trie.search("app"); // 返回 True
9494

9595
若搜索到了前缀的末尾,就说明字典树中存在该前缀。此外,若前缀末尾对应节点的 $isEnd$ 为真,则说明字典树中存在该字符串。
9696

97+
时间复杂度方面,插入字符串的时间复杂度为 $O(m \times |\Sigma|)$,查找前缀的时间复杂度为 $O(m)$,其中 $m$ 为字符串的长度,而 $|\Sigma|$ 为字符集的大小(本题中为 $26$)。空间复杂度为 $O(q \times m \times |\Sigma|)$,其中 $q$ 为插入的字符串数量。
98+
9799
<!-- tabs:start -->
98100

99101
#### Python3

solution/0200-0299/0208.Implement Trie (Prefix Tree)/README_EN.md

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,34 @@ trie.search(&quot;app&quot;); // return True
6565

6666
<!-- solution:start -->
6767

68-
### Solution 1
68+
## Solution 1: Trie (Prefix Tree)
69+
70+
Each node in the trie contains two parts:
71+
72+
1. An array of pointers to child nodes `children`. For this problem, the array length is 26, representing the number of lowercase English letters. `children[0]` corresponds to lowercase letter 'a', ..., and `children[25]` corresponds to lowercase letter 'z'.
73+
2. A boolean field `isEnd` indicating whether the node is the end of a string.
74+
75+
### 1. Insert a String
76+
77+
We start from the root of the trie and insert the string. For the child node corresponding to the current character, there are two cases:
78+
79+
- The child node exists. Move along the pointer to the child node and continue processing the next character.
80+
- The child node does not exist. Create a new child node, record it in the corresponding position of the `children` array, then move along the pointer to the child node and continue searching for the next character.
81+
82+
Repeat the above steps until the last character of the string is processed, then mark the current node as the end of the string.
83+
84+
### 2. Search for a Prefix
85+
86+
We start from the root of the trie and search for the prefix. For the child node corresponding to the current character, there are two cases:
87+
88+
- The child node exists. Move along the pointer to the child node and continue searching for the next character.
89+
- The child node does not exist. This means the trie does not contain the prefix, so return a null pointer.
90+
91+
Repeat the above steps until a null pointer is returned or the last character of the prefix is searched.
92+
93+
If we search to the end of the prefix, it means the trie contains the prefix. Additionally, if the `isEnd` of the node corresponding to the end of the prefix is true, it means the trie contains the string.
94+
95+
The time complexity for inserting a string is $O(m \times |\Sigma|)$, and the time complexity for searching a prefix is $O(m)$, where $m$ is the length of the string and $|\Sigma|$ is the size of the character set (26 in this problem). The space complexity is $O(q \times m \times |\Sigma|)$, where $q$ is the number of inserted strings.
6996

7097
<!-- tabs:start -->
7198

solution/0200-0299/0257.Binary Tree Paths/README_EN.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@ tags:
5353

5454
<!-- solution:start -->
5555

56-
### Solution 1
56+
## Solution 1: DFS
57+
58+
We can use depth-first search to traverse the entire binary tree. Each time, we add the current node to the path. If the current node is a leaf node, we add the entire path to the answer. Otherwise, we continue to recursively traverse the child nodes of the node. Finally, when the recursion ends and returns to the current node, we need to remove the current node from the path.
59+
60+
The time complexity is $O(n^2)$, and the space complexity is $O(n)$. Where $n$ is the number of nodes in the binary tree.
5761

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

solution/0300-0399/0334.Increasing Triplet Subsequence/README.md

Lines changed: 8 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -90,21 +90,16 @@ class Solution:
9090
```java
9191
class Solution {
9292
public boolean increasingTriplet(int[] nums) {
93-
int n = nums.length;
94-
int[] lmi = new int[n];
95-
int[] rmx = new int[n];
96-
lmi[0] = Integer.MAX_VALUE;
97-
rmx[n - 1] = Integer.MIN_VALUE;
98-
for (int i = 1; i < n; ++i) {
99-
lmi[i] = Math.min(lmi[i - 1], nums[i - 1]);
100-
}
101-
for (int i = n - 2; i >= 0; --i) {
102-
rmx[i] = Math.max(rmx[i + 1], nums[i + 1]);
103-
}
104-
for (int i = 0; i < n; ++i) {
105-
if (lmi[i] < nums[i] && nums[i] < rmx[i]) {
93+
int min = Integer.MAX_VALUE, mid = Integer.MAX_VALUE;
94+
for (int num : nums) {
95+
if (num > mid) {
10696
return true;
10797
}
98+
if (num <= min) {
99+
min = num;
100+
} else {
101+
mid = num;
102+
}
108103
}
109104
return false;
110105
}
@@ -199,35 +194,4 @@ impl Solution {
199194

200195
<!-- solution:end -->
201196

202-
<!-- solution:start -->
203-
204-
### 方法二
205-
206-
<!-- tabs:start -->
207-
208-
#### Java
209-
210-
```java
211-
class Solution {
212-
public boolean increasingTriplet(int[] nums) {
213-
int min = Integer.MAX_VALUE, mid = Integer.MAX_VALUE;
214-
for (int num : nums) {
215-
if (num > mid) {
216-
return true;
217-
}
218-
if (num <= min) {
219-
min = num;
220-
} else {
221-
mid = num;
222-
}
223-
}
224-
return false;
225-
}
226-
}
227-
```
228-
229-
<!-- tabs:end -->
230-
231-
<!-- solution:end -->
232-
233197
<!-- problem:end -->

solution/0300-0399/0334.Increasing Triplet Subsequence/README_EN.md

Lines changed: 8 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -86,21 +86,16 @@ class Solution:
8686
```java
8787
class Solution {
8888
public boolean increasingTriplet(int[] nums) {
89-
int n = nums.length;
90-
int[] lmi = new int[n];
91-
int[] rmx = new int[n];
92-
lmi[0] = Integer.MAX_VALUE;
93-
rmx[n - 1] = Integer.MIN_VALUE;
94-
for (int i = 1; i < n; ++i) {
95-
lmi[i] = Math.min(lmi[i - 1], nums[i - 1]);
96-
}
97-
for (int i = n - 2; i >= 0; --i) {
98-
rmx[i] = Math.max(rmx[i + 1], nums[i + 1]);
99-
}
100-
for (int i = 0; i < n; ++i) {
101-
if (lmi[i] < nums[i] && nums[i] < rmx[i]) {
89+
int min = Integer.MAX_VALUE, mid = Integer.MAX_VALUE;
90+
for (int num : nums) {
91+
if (num > mid) {
10292
return true;
10393
}
94+
if (num <= min) {
95+
min = num;
96+
} else {
97+
mid = num;
98+
}
10499
}
105100
return false;
106101
}
@@ -195,35 +190,4 @@ impl Solution {
195190

196191
<!-- solution:end -->
197192

198-
<!-- solution:start -->
199-
200-
### Solution 2
201-
202-
<!-- tabs:start -->
203-
204-
#### Java
205-
206-
```java
207-
class Solution {
208-
public boolean increasingTriplet(int[] nums) {
209-
int min = Integer.MAX_VALUE, mid = Integer.MAX_VALUE;
210-
for (int num : nums) {
211-
if (num > mid) {
212-
return true;
213-
}
214-
if (num <= min) {
215-
min = num;
216-
} else {
217-
mid = num;
218-
}
219-
}
220-
return false;
221-
}
222-
}
223-
```
224-
225-
<!-- tabs:end -->
226-
227-
<!-- solution:end -->
228-
229193
<!-- problem:end -->
Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
11
class Solution {
22
public boolean increasingTriplet(int[] nums) {
3-
int n = nums.length;
4-
int[] lmi = new int[n];
5-
int[] rmx = new int[n];
6-
lmi[0] = Integer.MAX_VALUE;
7-
rmx[n - 1] = Integer.MIN_VALUE;
8-
for (int i = 1; i < n; ++i) {
9-
lmi[i] = Math.min(lmi[i - 1], nums[i - 1]);
10-
}
11-
for (int i = n - 2; i >= 0; --i) {
12-
rmx[i] = Math.max(rmx[i + 1], nums[i + 1]);
13-
}
14-
for (int i = 0; i < n; ++i) {
15-
if (lmi[i] < nums[i] && nums[i] < rmx[i]) {
3+
int min = Integer.MAX_VALUE, mid = Integer.MAX_VALUE;
4+
for (int num : nums) {
5+
if (num > mid) {
166
return true;
177
}
8+
if (num <= min) {
9+
min = num;
10+
} else {
11+
mid = num;
12+
}
1813
}
1914
return false;
2015
}
21-
}
16+
}

solution/0300-0399/0334.Increasing Triplet Subsequence/Solution2.java

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)