Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ trie.search("app"); // 返回 True

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

时间复杂度方面,插入字符串的时间复杂度为 $O(m \times |\Sigma|)$,查找前缀的时间复杂度为 $O(m)$,其中 $m$ 为字符串的长度,而 $|\Sigma|$ 为字符集的大小(本题中为 $26$)。空间复杂度为 $O(q \times m \times |\Sigma|)$,其中 $q$ 为插入的字符串数量。

<!-- tabs:start -->

#### Python3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,34 @@ trie.search(&quot;app&quot;); // return True

<!-- solution:start -->

### Solution 1
## Solution 1: Trie (Prefix Tree)

Each node in the trie contains two parts:

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'.
2. A boolean field `isEnd` indicating whether the node is the end of a string.

### 1. Insert a String

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:

- The child node exists. Move along the pointer to the child node and continue processing the next character.
- 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.

Repeat the above steps until the last character of the string is processed, then mark the current node as the end of the string.

### 2. Search for a Prefix

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:

- The child node exists. Move along the pointer to the child node and continue searching for the next character.
- The child node does not exist. This means the trie does not contain the prefix, so return a null pointer.

Repeat the above steps until a null pointer is returned or the last character of the prefix is searched.

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.

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.

<!-- tabs:start -->

Expand Down
6 changes: 5 additions & 1 deletion solution/0200-0299/0257.Binary Tree Paths/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ tags:

<!-- solution:start -->

### Solution 1
## Solution 1: DFS

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.

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.

<!-- tabs:start -->

Expand Down
52 changes: 8 additions & 44 deletions solution/0300-0399/0334.Increasing Triplet Subsequence/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,21 +90,16 @@ class Solution:
```java
class Solution {
public boolean increasingTriplet(int[] nums) {
int n = nums.length;
int[] lmi = new int[n];
int[] rmx = new int[n];
lmi[0] = Integer.MAX_VALUE;
rmx[n - 1] = Integer.MIN_VALUE;
for (int i = 1; i < n; ++i) {
lmi[i] = Math.min(lmi[i - 1], nums[i - 1]);
}
for (int i = n - 2; i >= 0; --i) {
rmx[i] = Math.max(rmx[i + 1], nums[i + 1]);
}
for (int i = 0; i < n; ++i) {
if (lmi[i] < nums[i] && nums[i] < rmx[i]) {
int min = Integer.MAX_VALUE, mid = Integer.MAX_VALUE;
for (int num : nums) {
if (num > mid) {
return true;
}
if (num <= min) {
min = num;
} else {
mid = num;
}
}
return false;
}
Expand Down Expand Up @@ -199,35 +194,4 @@ impl Solution {

<!-- solution:end -->

<!-- solution:start -->

### 方法二

<!-- tabs:start -->

#### Java

```java
class Solution {
public boolean increasingTriplet(int[] nums) {
int min = Integer.MAX_VALUE, mid = Integer.MAX_VALUE;
for (int num : nums) {
if (num > mid) {
return true;
}
if (num <= min) {
min = num;
} else {
mid = num;
}
}
return false;
}
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
Original file line number Diff line number Diff line change
Expand Up @@ -86,21 +86,16 @@ class Solution:
```java
class Solution {
public boolean increasingTriplet(int[] nums) {
int n = nums.length;
int[] lmi = new int[n];
int[] rmx = new int[n];
lmi[0] = Integer.MAX_VALUE;
rmx[n - 1] = Integer.MIN_VALUE;
for (int i = 1; i < n; ++i) {
lmi[i] = Math.min(lmi[i - 1], nums[i - 1]);
}
for (int i = n - 2; i >= 0; --i) {
rmx[i] = Math.max(rmx[i + 1], nums[i + 1]);
}
for (int i = 0; i < n; ++i) {
if (lmi[i] < nums[i] && nums[i] < rmx[i]) {
int min = Integer.MAX_VALUE, mid = Integer.MAX_VALUE;
for (int num : nums) {
if (num > mid) {
return true;
}
if (num <= min) {
min = num;
} else {
mid = num;
}
}
return false;
}
Expand Down Expand Up @@ -195,35 +190,4 @@ impl Solution {

<!-- solution:end -->

<!-- solution:start -->

### Solution 2

<!-- tabs:start -->

#### Java

```java
class Solution {
public boolean increasingTriplet(int[] nums) {
int min = Integer.MAX_VALUE, mid = Integer.MAX_VALUE;
for (int num : nums) {
if (num > mid) {
return true;
}
if (num <= min) {
min = num;
} else {
mid = num;
}
}
return false;
}
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
class Solution {
public boolean increasingTriplet(int[] nums) {
int n = nums.length;
int[] lmi = new int[n];
int[] rmx = new int[n];
lmi[0] = Integer.MAX_VALUE;
rmx[n - 1] = Integer.MIN_VALUE;
for (int i = 1; i < n; ++i) {
lmi[i] = Math.min(lmi[i - 1], nums[i - 1]);
}
for (int i = n - 2; i >= 0; --i) {
rmx[i] = Math.max(rmx[i + 1], nums[i + 1]);
}
for (int i = 0; i < n; ++i) {
if (lmi[i] < nums[i] && nums[i] < rmx[i]) {
int min = Integer.MAX_VALUE, mid = Integer.MAX_VALUE;
for (int num : nums) {
if (num > mid) {
return true;
}
if (num <= min) {
min = num;
} else {
mid = num;
}
}
return false;
}
}
}

This file was deleted.