From 59b38a9ebf73af718de50c7cc11b92bc5ef0ad68 Mon Sep 17 00:00:00 2001 From: bqlin Date: Sun, 19 Dec 2021 17:04:04 +0800 Subject: [PATCH 1/8] =?UTF-8?q?1002.=E6=9F=A5=E6=89=BE=E5=B8=B8=E7=94=A8?= =?UTF-8?q?=E5=AD=97=E7=AC=A6=EF=BC=9A=E4=BC=98=E5=8C=96=E6=8E=92=E7=89=88?= =?UTF-8?q?=EF=BC=8C=E4=BF=AE=E6=94=B9=E9=94=99=E5=88=AB=E5=AD=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/1002.查找常用字符.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/problems/1002.查找常用字符.md b/problems/1002.查找常用字符.md index 08e7ec3d..7c5566d3 100644 --- a/problems/1002.查找常用字符.md +++ b/problems/1002.查找常用字符.md @@ -58,7 +58,7 @@ words[i] 由小写英文字母组成 先统计第一个字符串所有字符出现的次数,代码如下: -``` +```cpp int hash[26] = {0}; // 用来统计所有字符串里字符出现的最小频率 for (int i = 0; i < A[0].size(); i++) { // 用第一个字符串给hash初始化 hash[A[0][i] - 'a']++; @@ -71,7 +71,7 @@ for (int i = 0; i < A[0].size(); i++) { // 用第一个字符串给hash初始化 代码如下: -``` +```cpp int hashOtherStr[26] = {0}; // 统计除第一个字符串外字符的出现频率 for (int i = 1; i < A.size(); i++) { memset(hashOtherStr, 0, 26 * sizeof(int)); @@ -84,11 +84,11 @@ for (int i = 1; i < A.size(); i++) { } } ``` -此时hash里统计着字符在所有字符串里出现的最小次数,那么把hash转正题目要求的输出格式就可以了。 +此时hash里统计着字符在所有字符串里出现的最小次数,那么把hash转成题目要求的输出格式就可以了。 代码如下: -``` +```cpp // 将hash统计的字符次数,转成输出形式 for (int i = 0; i < 26; i++) { while (hash[i] != 0) { // 注意这里是while,多个重复的字符 From 3078fb8eeaaa0a7e4200e6a0c2eb5e7835ea5bb4 Mon Sep 17 00:00:00 2001 From: bqlin Date: Sun, 19 Dec 2021 17:22:04 +0800 Subject: [PATCH 2/8] =?UTF-8?q?0001.=E4=B8=A4=E6=95=B0=E4=B9=8B=E5=92=8C?= =?UTF-8?q?=EF=BC=9A=E7=AE=80=E5=8C=96Swift=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0001.两数之和.md | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/problems/0001.两数之和.md b/problems/0001.两数之和.md index 37c95736..8551274c 100644 --- a/problems/0001.两数之和.md +++ b/problems/0001.两数之和.md @@ -207,18 +207,16 @@ function twoSum(array $nums, int $target): array Swift: ```swift func twoSum(_ nums: [Int], _ target: Int) -> [Int] { - var res = [Int]() - var dict = [Int : Int]() - for i in 0 ..< nums.count { - let other = target - nums[i] - if dict.keys.contains(other) { - res.append(i) - res.append(dict[other]!) - return res + // 值: 下标 + var map = [Int: Int]() + for (i, e) in nums.enumerated() { + if let v = map[target - e] { + return [v, i] + } else { + map[e] = i } - dict[nums[i]] = i } - return res + return [] } ``` From 6144420c3962f2e63a4e682baa5e7eecce10b08c Mon Sep 17 00:00:00 2001 From: bqlin Date: Sun, 19 Dec 2021 17:28:51 +0800 Subject: [PATCH 3/8] =?UTF-8?q?0454.=E5=9B=9B=E6=95=B0=E7=9B=B8=E5=8A=A0II?= =?UTF-8?q?=EF=BC=9A=E7=AE=80=E5=8C=96Swift=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0454.四数相加II.md | 36 +++++++++++++++------------------ 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/problems/0454.四数相加II.md b/problems/0454.四数相加II.md index 6853354c..6e916608 100644 --- a/problems/0454.四数相加II.md +++ b/problems/0454.四数相加II.md @@ -44,7 +44,7 @@ D = [ 0, 2] 1. 首先定义 一个unordered_map,key放a和b两数之和,value 放a和b两数之和出现的次数。 2. 遍历大A和大B数组,统计两个数组元素之和,和出现的次数,放到map中。 -3. 定义int变量count,用来统计a+b+c+d = 0 出现的次数。 +3. 定义int变量count,用来统计 a+b+c+d = 0 出现的次数。 4. 在遍历大C和大D数组,找到如果 0-(c+d) 在map中出现过的话,就用count把map中key对应的value也就是出现次数统计出来。 5. 最后返回统计值 count 就可以了 @@ -139,7 +139,7 @@ class Solution(object): return count -``` +``` Go: ```go @@ -229,28 +229,24 @@ class Solution { Swift: ```swift func fourSumCount(_ nums1: [Int], _ nums2: [Int], _ nums3: [Int], _ nums4: [Int]) -> Int { - // key:a+b的数值,value:a+b数值出现的次数 - var map = [Int: Int]() - // 遍历nums1和nums2数组,统计两个数组元素之和,和出现的次数,放到map中 - for i in 0 ..< nums1.count { - for j in 0 ..< nums2.count { - let sum1 = nums1[i] + nums2[j] - map[sum1] = (map[sum1] ?? 0) + 1 + // ab和: ab和出现次数 + var countDic = [Int: Int]() + for a in nums1 { + for b in nums2 { + let key = a + b + countDic[key] = countDic[key, default: 0] + 1 } } - // 统计a+b+c+d = 0 出现的次数 - var res = 0 - // 在遍历大num3和num4数组,找到如果 0-(c+d) 在map中出现过的话,就把map中key对应的value也就是出现次数统计出来。 - for i in 0 ..< nums3.count { - for j in 0 ..< nums4.count { - let sum2 = nums3[i] + nums4[j] - let other = 0 - sum2 - if map.keys.contains(other) { - res += map[other]! - } + + // 通过-(c + d)作为key,去累加ab和出现的次数 + var result = 0 + for c in nums3 { + for d in nums4 { + let key = -(c + d) + result += countDic[key, default: 0] } } - return res + return result } ``` From 1104815b1885d5ce50ff9cd5006a3ee9caaa6a94 Mon Sep 17 00:00:00 2001 From: bqlin Date: Sun, 19 Dec 2021 18:58:57 +0800 Subject: [PATCH 4/8] =?UTF-8?q?=E2=80=9C=E5=80=92=E5=8F=99=E2=80=9D=20->?= =?UTF-8?q?=20=E2=80=9C=E5=80=92=E5=BA=8F=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0031.下一个排列.md | 2 +- problems/0151.翻转字符串里的单词.md | 2 +- problems/0344.反转字符串.md | 1 - problems/0347.前K个高频元素.md | 4 ++-- problems/0416.分割等和子集.md | 2 +- problems/0685.冗余连接II.md | 4 ++-- problems/0746.使用最小花费爬楼梯.md | 2 +- .../1047.删除字符串中的所有相邻重复项.md | 2 +- problems/1049.最后一块石头的重量II.md | 2 +- problems/背包理论基础01背包-2.md | 8 ++++---- 10 files changed, 14 insertions(+), 15 deletions(-) diff --git a/problems/0031.下一个排列.md b/problems/0031.下一个排列.md index 10ee0aaa..84bf3e60 100644 --- a/problems/0031.下一个排列.md +++ b/problems/0031.下一个排列.md @@ -86,7 +86,7 @@ public: } } } - // 到这里了说明整个数组都是倒叙了,反转一下便可 + // 到这里了说明整个数组都是倒序了,反转一下便可 reverse(nums.begin(), nums.end()); } }; diff --git a/problems/0151.翻转字符串里的单词.md b/problems/0151.翻转字符串里的单词.md index 48324cd9..d58ace89 100644 --- a/problems/0151.翻转字符串里的单词.md +++ b/problems/0151.翻转字符串里的单词.md @@ -40,7 +40,7 @@ 不能使用辅助空间之后,那么只能在原字符串上下功夫了。 -想一下,我们将整个字符串都反转过来,那么单词的顺序指定是倒序了,只不过单词本身也倒叙了,那么再把单词反转一下,单词不就正过来了。 +想一下,我们将整个字符串都反转过来,那么单词的顺序指定是倒序了,只不过单词本身也倒序了,那么再把单词反转一下,单词不就正过来了。 所以解题思路如下: diff --git a/problems/0344.反转字符串.md b/problems/0344.反转字符串.md index 28313839..9176c915 100644 --- a/problems/0344.反转字符串.md +++ b/problems/0344.反转字符串.md @@ -101,7 +101,6 @@ s[j] = tmp; s[i] ^= s[j]; s[j] ^= s[i]; s[i] ^= s[j]; - ``` 这道题目还是比较简单的,但是我正好可以通过这道题目说一说在刷题的时候,使用库函数的原则。 diff --git a/problems/0347.前K个高频元素.md b/problems/0347.前K个高频元素.md index e6ad751b..8bd774e9 100644 --- a/problems/0347.前K个高频元素.md +++ b/problems/0347.前K个高频元素.md @@ -107,7 +107,7 @@ public: } } - // 找出前K个高频元素,因为小顶堆先弹出的是最小的,所以倒叙来输出到数组 + // 找出前K个高频元素,因为小顶堆先弹出的是最小的,所以倒序来输出到数组 vector result(k); for (int i = k - 1; i >= 0; i--) { result[i] = pri_que.top().first; @@ -180,7 +180,7 @@ class Solution: if len(pri_que) > k: #如果堆的大小大于了K,则队列弹出,保证堆的大小一直为k heapq.heappop(pri_que) - #找出前K个高频元素,因为小顶堆先弹出的是最小的,所以倒叙来输出到数组 + #找出前K个高频元素,因为小顶堆先弹出的是最小的,所以倒序来输出到数组 result = [0] * k for i in range(k-1, -1, -1): result[i] = heapq.heappop(pri_que)[1] diff --git a/problems/0416.分割等和子集.md b/problems/0416.分割等和子集.md index 23a285f8..45b8b416 100644 --- a/problems/0416.分割等和子集.md +++ b/problems/0416.分割等和子集.md @@ -112,7 +112,7 @@ vector dp(10001, 0); 4. 确定遍历顺序 -在[动态规划:关于01背包问题,你该了解这些!(滚动数组)](https://programmercarl.com/背包理论基础01背包-2.html)中就已经说明:如果使用一维dp数组,物品遍历的for循环放在外层,遍历背包的for循环放在内层,且内层for循环倒叙遍历! +在[动态规划:关于01背包问题,你该了解这些!(滚动数组)](https://programmercarl.com/背包理论基础01背包-2.html)中就已经说明:如果使用一维dp数组,物品遍历的for循环放在外层,遍历背包的for循环放在内层,且内层for循环倒序遍历! 代码如下: diff --git a/problems/0685.冗余连接II.md b/problems/0685.冗余连接II.md index de62432c..d96d4912 100644 --- a/problems/0685.冗余连接II.md +++ b/problems/0685.冗余连接II.md @@ -68,7 +68,7 @@ for (int i = 0; i < n; i++) { ```cpp vector vec; // 记录入度为2的边(如果有的话就两条边) -// 找入度为2的节点所对应的边,注意要倒叙,因为优先返回最后出现在二维数组中的答案 +// 找入度为2的节点所对应的边,注意要倒序,因为优先返回最后出现在二维数组中的答案 for (int i = n - 1; i >= 0; i--) { if (inDegree[edges[i][1]] == 2) { vec.push_back(i); @@ -577,7 +577,7 @@ var findRedundantDirectedConnection = function(edges) { inDegree[edges[i][1]]++; // 统计入度 } let vec = [];// 记录入度为2的边(如果有的话就两条边) - // 找入度为2的节点所对应的边,注意要倒叙,因为优先返回最后出现在二维数组中的答案 + // 找入度为2的节点所对应的边,注意要倒序,因为优先返回最后出现在二维数组中的答案 for (let i = n - 1; i >= 0; i--) { if (inDegree[edges[i][1]] == 2) { vec.push(i); diff --git a/problems/0746.使用最小花费爬楼梯.md b/problems/0746.使用最小花费爬楼梯.md index a146cc8b..0009f06c 100644 --- a/problems/0746.使用最小花费爬楼梯.md +++ b/problems/0746.使用最小花费爬楼梯.md @@ -82,7 +82,7 @@ dp[1] = cost[1]; **但是稍稍有点难度的动态规划,其遍历顺序并不容易确定下来**。 -例如:01背包,都知道两个for循环,一个for遍历物品嵌套一个for遍历背包容量,那么为什么不是一个for遍历背包容量嵌套一个for遍历物品呢? 以及在使用一维dp数组的时候遍历背包容量为什么要倒叙呢? +例如:01背包,都知道两个for循环,一个for遍历物品嵌套一个for遍历背包容量,那么为什么不是一个for遍历背包容量嵌套一个for遍历物品呢? 以及在使用一维dp数组的时候遍历背包容量为什么要倒序呢? **这些都是遍历顺序息息相关。当然背包问题后续「代码随想录」都会重点讲解的!** diff --git a/problems/1047.删除字符串中的所有相邻重复项.md b/problems/1047.删除字符串中的所有相邻重复项.md index 755520f3..d6eefd07 100644 --- a/problems/1047.删除字符串中的所有相邻重复项.md +++ b/problems/1047.删除字符串中的所有相邻重复项.md @@ -63,7 +63,7 @@ ![1047.删除字符串中的所有相邻重复项](https://code-thinking.cdn.bcebos.com/gifs/1047.删除字符串中的所有相邻重复项.gif) -从栈中弹出剩余元素,此时是字符串ac,因为从栈里弹出的元素是倒叙的,所以在对字符串进行反转一下,就得到了最终的结果。 +从栈中弹出剩余元素,此时是字符串ac,因为从栈里弹出的元素是倒序的,所以在对字符串进行反转一下,就得到了最终的结果。 C++代码 : diff --git a/problems/1049.最后一块石头的重量II.md b/problems/1049.最后一块石头的重量II.md index 975968de..d64e7e56 100644 --- a/problems/1049.最后一块石头的重量II.md +++ b/problems/1049.最后一块石头的重量II.md @@ -87,7 +87,7 @@ vector dp(15001, 0); 4. 确定遍历顺序 -在[动态规划:关于01背包问题,你该了解这些!(滚动数组)](https://programmercarl.com/背包理论基础01背包-2.html)中就已经说明:如果使用一维dp数组,物品遍历的for循环放在外层,遍历背包的for循环放在内层,且内层for循环倒叙遍历! +在[动态规划:关于01背包问题,你该了解这些!(滚动数组)](https://programmercarl.com/背包理论基础01背包-2.html)中就已经说明:如果使用一维dp数组,物品遍历的for循环放在外层,遍历背包的for循环放在内层,且内层for循环倒序遍历! 代码如下: diff --git a/problems/背包理论基础01背包-2.md b/problems/背包理论基础01背包-2.md index 7f646f06..a57bae10 100644 --- a/problems/背包理论基础01背包-2.md +++ b/problems/背包理论基础01背包-2.md @@ -103,7 +103,7 @@ for(int i = 0; i < weight.size(); i++) { // 遍历物品 为什么呢? -**倒叙遍历是为了保证物品i只被放入一次!**。但如果一旦正序遍历了,那么物品0就会被重复加入多次! +**倒序遍历是为了保证物品i只被放入一次!**。但如果一旦正序遍历了,那么物品0就会被重复加入多次! 举一个例子:物品0的重量weight[0] = 1,价值value[0] = 15 @@ -115,9 +115,9 @@ dp[2] = dp[2 - weight[0]] + value[0] = 30 此时dp[2]就已经是30了,意味着物品0,被放入了两次,所以不能正序遍历。 -为什么倒叙遍历,就可以保证物品只放入一次呢? +为什么倒序遍历,就可以保证物品只放入一次呢? -倒叙就是先算dp[2] +倒序就是先算dp[2] dp[2] = dp[2 - weight[0]] + value[0] = 15 (dp数组已经都初始化为0) @@ -125,7 +125,7 @@ dp[1] = dp[1 - weight[0]] + value[0] = 15 所以从后往前循环,每次取得状态不会和之前取得状态重合,这样每种物品就只取一次了。 -**那么问题又来了,为什么二维dp数组历的时候不用倒叙呢?** +**那么问题又来了,为什么二维dp数组历的时候不用倒序呢?** 因为对于二维dp,dp[i][j]都是通过上一层即dp[i - 1][j]计算而来,本层的dp[i][j]并不会被覆盖! From cb0f5906fdf7a22a097958cbb89783905baf970f Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Sat, 8 Jan 2022 23:55:34 +0800 Subject: [PATCH 5/8] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=88=E9=93=BE?= =?UTF-8?q?=E8=A1=A8=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=80.md=EF=BC=89?= =?UTF-8?q?=EF=BC=9A=E5=A2=9E=E5=8A=A0JS=E5=92=8CTS=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/链表理论基础.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/problems/链表理论基础.md b/problems/链表理论基础.md index 02b7029e..109aa1ed 100644 --- a/problems/链表理论基础.md +++ b/problems/链表理论基础.md @@ -168,6 +168,32 @@ public class ListNode { } ``` +JavaScript: + +```javascript +class ListNode { + val; + next = null; + constructor(value) { + this.val = value; + this.next = null; + } +} +``` + +TypeScript: + +```typescript +class ListNode { + public val: number; + public next: ListNode = null; + constructor(value: number) { + this.val = value; + this.next = null; + } +} +``` + Python: From 1c595ed05192de37b7c9927b826a365c96ed2fae Mon Sep 17 00:00:00 2001 From: Du Date: Sun, 9 Jan 2022 08:52:20 +0800 Subject: [PATCH 6/8] =?UTF-8?q?=E6=B7=BB=E5=8A=A0105.=20=E4=BB=8E=E5=89=8D?= =?UTF-8?q?=E5=BA=8F=E4=B8=8E=E4=B8=AD=E5=BA=8F=E9=81=8D=E5=8E=86=E5=BA=8F?= =?UTF-8?q?=E5=88=97=E6=9E=84=E9=80=A0=E4=BA=8C=E5=8F=89=E6=A0=91=E3=80=81?= =?UTF-8?q?106.=20=E4=BB=8E=E4=B8=AD=E5=BA=8F=E4=B8=8E=E5=90=8E=E5=BA=8F?= =?UTF-8?q?=E9=81=8D=E5=8E=86=E5=BA=8F=E5=88=97=E6=9E=84=E9=80=A0=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=20Swift=20=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...序与后序遍历序列构造二叉树.md | 102 +++++++++++++++++- 1 file changed, 100 insertions(+), 2 deletions(-) diff --git a/problems/0106.从中序与后序遍历序列构造二叉树.md b/problems/0106.从中序与后序遍历序列构造二叉树.md index 487e4f7d..59e0ba53 100644 --- a/problems/0106.从中序与后序遍历序列构造二叉树.md +++ b/problems/0106.从中序与后序遍历序列构造二叉树.md @@ -684,7 +684,7 @@ class Solution: root.right = self.buildTree(preorder_right, inorder_right) return root -``` +``` 106.从中序与后序遍历序列构造二叉树 @@ -716,7 +716,7 @@ class Solution: root.right = self.buildTree(inorder_right, postorder_right) return root -``` +``` ## Go @@ -816,6 +816,7 @@ var buildTree = function(preorder, inorder) { ## C 106 从中序与后序遍历序列构造二叉树 + ```c int linearSearch(int* arr, int arrSize, int key) { int i; @@ -847,6 +848,7 @@ struct TreeNode* buildTree(int* inorder, int inorderSize, int* postorder, int po ``` 105 从前序与中序遍历序列构造二叉树 + ```c struct TreeNode* buildTree(int* preorder, int preorderSize, int* inorder, int inorderSize){ // 递归结束条件:传入的数组大小为0 @@ -887,7 +889,103 @@ struct TreeNode* buildTree(int* preorder, int preorderSize, int* inorder, int in // 6.返回根节点 return root; } + ``` +## Swift + +105 从前序与中序遍历序列构造二叉树 + +```swift +class Solution { + func buildTree(_ preorder: [Int], _ inorder: [Int]) -> TreeNode? { + return helper(preorder: preorder, + preorderBegin: 0, + preorderEnd: preorder.count, + inorder: inorder, + inorderBegin: 0, + inorderEnd: inorder.count) + } + + func helper(preorder: [Int], preorderBegin: Int, preorderEnd: Int, inorder: [Int], inorderBegin: Int, inorderEnd: Int) -> TreeNode? { + if preorderBegin == preorderEnd { + return nil + } + + // 前序遍历数组的第一个元素作为分割点 + let rootValue = preorder[preorderBegin] + let root = TreeNode(rootValue) + + + if preorderEnd - preorderBegin == 1 { + return root + } + + var index = 0 // 从中序遍历数组中找到根节点的下标 + if let ind = inorder.firstIndex(of: rootValue) { + index = ind + } + + // 递归 + root.left = helper(preorder: preorder, + preorderBegin: preorderBegin + 1, + preorderEnd: preorderBegin + 1 + index - inorderBegin, + inorder: inorder, + inorderBegin: inorderBegin, + inorderEnd: index) + root.right = helper(preorder: preorder, + preorderBegin: preorderBegin + 1 + index - inorderBegin, + preorderEnd: preorderEnd, + inorder: inorder, + inorderBegin: index + 1, + inorderEnd: inorderEnd) + return root + } +} +``` + +106 从中序与后序遍历序列构造二叉树 + +```swift +class Solution_0106 { + func buildTree(inorder: [Int], inorderBegin: Int, inorderEnd: Int, postorder: [Int], postorderBegin: Int, postorderEnd: Int) -> TreeNode? { + if postorderEnd - postorderBegin < 1 { + return nil + } + + // 后序遍历数组的最后一个元素作为分割点 + let rootValue = postorder[postorderEnd - 1] + let root = TreeNode(rootValue) + + if postorderEnd - postorderBegin == 1 { + return root + } + + // 从中序遍历数组中找到根节点的下标 + var delimiterIndex = 0 + if let index = inorder.firstIndex(of: rootValue) { + delimiterIndex = index + } + + root.left = buildTree(inorder: inorder, + inorderBegin: inorderBegin, + inorderEnd: delimiterIndex, + postorder: postorder, + postorderBegin: postorderBegin, + postorderEnd: postorderBegin + (delimiterIndex - inorderBegin)) + + root.right = buildTree(inorder: inorder, + inorderBegin: delimiterIndex + 1, + inorderEnd: inorderEnd, + postorder: postorder, + postorderBegin: postorderBegin + (delimiterIndex - inorderBegin), + postorderEnd: postorderEnd - 1) + return root + } +} +``` + + + -----------------------
From 1f7f1617296bbb862b5ff6a265659ccd53659af4 Mon Sep 17 00:00:00 2001 From: Du Date: Sun, 9 Jan 2022 08:57:44 +0800 Subject: [PATCH 7/8] =?UTF-8?q?=E6=B7=BB=E5=8A=A0105.=20=E4=BB=8E=E5=89=8D?= =?UTF-8?q?=E5=BA=8F=E4=B8=8E=E4=B8=AD=E5=BA=8F=E9=81=8D=E5=8E=86=E5=BA=8F?= =?UTF-8?q?=E5=88=97=E6=9E=84=E9=80=A0=E4=BA=8C=E5=8F=89=E6=A0=91=E3=80=81?= =?UTF-8?q?106.=20=E4=BB=8E=E4=B8=AD=E5=BA=8F=E4=B8=8E=E5=90=8E=E5=BA=8F?= =?UTF-8?q?=E9=81=8D=E5=8E=86=E5=BA=8F=E5=88=97=E6=9E=84=E9=80=A0=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=20Swift=20=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0106.从中序与后序遍历序列构造二叉树.md | 1 - 1 file changed, 1 deletion(-) diff --git a/problems/0106.从中序与后序遍历序列构造二叉树.md b/problems/0106.从中序与后序遍历序列构造二叉树.md index 59e0ba53..40d75983 100644 --- a/problems/0106.从中序与后序遍历序列构造二叉树.md +++ b/problems/0106.从中序与后序遍历序列构造二叉树.md @@ -889,7 +889,6 @@ struct TreeNode* buildTree(int* preorder, int preorderSize, int* inorder, int in // 6.返回根节点 return root; } - ``` ## Swift From a246b9b4e6dc344925259f64150400ddb7661ed4 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Sun, 9 Jan 2022 13:42:10 +0800 Subject: [PATCH 8/8] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880203.=E7=A7=BB?= =?UTF-8?q?=E9=99=A4=E9=93=BE=E8=A1=A8=E5=85=83=E7=B4=A0.md=EF=BC=89?= =?UTF-8?q?=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0203.移除链表元素.md | 56 +++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/problems/0203.移除链表元素.md b/problems/0203.移除链表元素.md index 9d0ef91f..4e7bdd34 100644 --- a/problems/0203.移除链表元素.md +++ b/problems/0203.移除链表元素.md @@ -302,7 +302,63 @@ var removeElements = function(head, val) { }; ``` +TypeScript: + +版本一(在原链表上直接删除): + +```typescript +/** + * Definition for singly-linked list. + * class ListNode { + * val: number + * next: ListNode | null + * constructor(val?: number, next?: ListNode | null) { + * this.val = (val===undefined ? 0 : val) + * this.next = (next===undefined ? null : next) + * } + * } + */ +function removeElements(head: ListNode | null, val: number): ListNode | null { + // 删除头部节点 + while (head !== null && head.val === val) { + head = head.next; + } + if (head === null) return head; + let pre: ListNode = head, cur: ListNode = head.next; + // 删除非头部节点 + while (cur) { + if (cur.val === val) { + pre.next = cur.next; + } else { + pre = pre.next; + } + cur = cur.next; + } + return head; +}; +``` + +版本二(虚拟头节点): + +```typescript +function removeElements(head: ListNode | null, val: number): ListNode | null { + head = new ListNode(0, head); + let pre: ListNode = head, cur: ListNode = head.next; + // 删除非头部节点 + while (cur) { + if (cur.val === val) { + pre.next = cur.next; + } else { + pre = pre.next; + } + cur = cur.next; + } + return head.next; +}; +``` + Swift: + ```swift /** * Definition for singly-linked list.