From 1b46b27fea652883ff987d1d2ff628c2fc242194 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Thu, 27 Nov 2025 07:40:52 +0800 Subject: [PATCH] feat: add solutions to lc problem --- .../README.md | 2 + .../README_EN.md | 29 ++++++++++- .../0257.Binary Tree Paths/README_EN.md | 6 ++- .../README.md | 52 +++---------------- .../README_EN.md | 52 +++---------------- .../Solution.java | 23 ++++---- .../Solution2.java | 16 ------ 7 files changed, 60 insertions(+), 120 deletions(-) delete mode 100644 solution/0300-0399/0334.Increasing Triplet Subsequence/Solution2.java diff --git a/solution/0200-0299/0208.Implement Trie (Prefix Tree)/README.md b/solution/0200-0299/0208.Implement Trie (Prefix Tree)/README.md index 76c12ffdbb115..3080b78b786bb 100644 --- a/solution/0200-0299/0208.Implement Trie (Prefix Tree)/README.md +++ b/solution/0200-0299/0208.Implement Trie (Prefix Tree)/README.md @@ -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$ 为插入的字符串数量。 + #### Python3 diff --git a/solution/0200-0299/0208.Implement Trie (Prefix Tree)/README_EN.md b/solution/0200-0299/0208.Implement Trie (Prefix Tree)/README_EN.md index 44a337cd122e8..20caf041f2d19 100644 --- a/solution/0200-0299/0208.Implement Trie (Prefix Tree)/README_EN.md +++ b/solution/0200-0299/0208.Implement Trie (Prefix Tree)/README_EN.md @@ -65,7 +65,34 @@ trie.search("app"); // return True -### 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. diff --git a/solution/0200-0299/0257.Binary Tree Paths/README_EN.md b/solution/0200-0299/0257.Binary Tree Paths/README_EN.md index 5815ea69ee308..d87cd7b47f4f8 100644 --- a/solution/0200-0299/0257.Binary Tree Paths/README_EN.md +++ b/solution/0200-0299/0257.Binary Tree Paths/README_EN.md @@ -53,7 +53,11 @@ tags: -### 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. diff --git a/solution/0300-0399/0334.Increasing Triplet Subsequence/README.md b/solution/0300-0399/0334.Increasing Triplet Subsequence/README.md index 96212cf016256..5c6baa98ddb97 100644 --- a/solution/0300-0399/0334.Increasing Triplet Subsequence/README.md +++ b/solution/0300-0399/0334.Increasing Triplet Subsequence/README.md @@ -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; } @@ -199,35 +194,4 @@ impl Solution { - - -### 方法二 - - - -#### 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; - } -} -``` - - - - - diff --git a/solution/0300-0399/0334.Increasing Triplet Subsequence/README_EN.md b/solution/0300-0399/0334.Increasing Triplet Subsequence/README_EN.md index 494b38a4030ea..9a34c6d1d8eda 100644 --- a/solution/0300-0399/0334.Increasing Triplet Subsequence/README_EN.md +++ b/solution/0300-0399/0334.Increasing Triplet Subsequence/README_EN.md @@ -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; } @@ -195,35 +190,4 @@ impl Solution { - - -### Solution 2 - - - -#### 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; - } -} -``` - - - - - diff --git a/solution/0300-0399/0334.Increasing Triplet Subsequence/Solution.java b/solution/0300-0399/0334.Increasing Triplet Subsequence/Solution.java index bb46c107370a8..7713813cfc779 100644 --- a/solution/0300-0399/0334.Increasing Triplet Subsequence/Solution.java +++ b/solution/0300-0399/0334.Increasing Triplet Subsequence/Solution.java @@ -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; } -} \ No newline at end of file +} diff --git a/solution/0300-0399/0334.Increasing Triplet Subsequence/Solution2.java b/solution/0300-0399/0334.Increasing Triplet Subsequence/Solution2.java deleted file mode 100644 index 6fc1f4bf77b71..0000000000000 --- a/solution/0300-0399/0334.Increasing Triplet Subsequence/Solution2.java +++ /dev/null @@ -1,16 +0,0 @@ -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; - } -} \ No newline at end of file