From 343ddb37f9e84284ff8df34f89bed18522244e43 Mon Sep 17 00:00:00 2001 From: bqlin Date: Mon, 20 Dec 2021 20:13:42 +0800 Subject: [PATCH 01/11] =?UTF-8?q?1047.=E5=88=A0=E9=99=A4=E5=AD=97=E7=AC=A6?= =?UTF-8?q?=E4=B8=B2=E4=B8=AD=E7=9A=84=E6=89=80=E6=9C=89=E7=9B=B8=E9=82=BB?= =?UTF-8?q?=E9=87=8D=E5=A4=8D=E9=A1=B9=EF=BC=9A=E4=BC=98=E5=8C=96Swift?= =?UTF-8?q?=E7=89=88=E6=9C=AC=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../1047.删除字符串中的所有相邻重复项.md | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/problems/1047.删除字符串中的所有相邻重复项.md b/problems/1047.删除字符串中的所有相邻重复项.md index d6eefd07..9309aed8 100644 --- a/problems/1047.删除字符串中的所有相邻重复项.md +++ b/problems/1047.删除字符串中的所有相邻重复项.md @@ -322,14 +322,12 @@ char * removeDuplicates(char * s){ Swift: ```swift func removeDuplicates(_ s: String) -> String { - let array = Array(s) var stack = [Character]() - for c in array { - let last: Character? = stack.last - if stack.isEmpty || last != c { - stack.append(c) - } else { + for c in s { + if stack.last == c { stack.removeLast() + } else { + stack.append(c) } } return String(stack) From 2aa31ce54bcb186376a1341d8ef91282a8db4c6f Mon Sep 17 00:00:00 2001 From: bqlin Date: Mon, 20 Dec 2021 21:42:07 +0800 Subject: [PATCH 02/11] =?UTF-8?q?0239.=E6=BB=91=E5=8A=A8=E7=AA=97=E5=8F=A3?= =?UTF-8?q?=E6=9C=80=E5=A4=A7=E5=80=BC=EF=BC=9A=E4=BC=98=E5=8C=96=E6=8E=92?= =?UTF-8?q?=E7=89=88=EF=BC=8C=E8=A1=A5=E5=85=85Swift=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0239.滑动窗口最大值.md | 36 +++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/problems/0239.滑动窗口最大值.md b/problems/0239.滑动窗口最大值.md index d9788f63..d7a94b5a 100644 --- a/problems/0239.滑动窗口最大值.md +++ b/problems/0239.滑动窗口最大值.md @@ -45,7 +45,7 @@ 这个队列应该长这个样子: -``` +```cpp class MyQueue { public: void pop(int value) { @@ -525,5 +525,39 @@ class Solution { } ``` +Swift解法二: + +```swift +func maxSlidingWindow(_ nums: [Int], _ k: Int) -> [Int] { + var result = [Int]() + var window = [Int]() + var right = 0, left = right - k + 1 + + while right < nums.count { + let value = nums[right] + + // 因为窗口移动丢弃的左边数 + if left > 0, left - 1 == window.first { + window.removeFirst() + } + + // 保证末尾的是最大的 + while !window.isEmpty, value > nums[window.last!] { + window.removeLast() + } + window.append(right) + + if left >= 0 { // 窗口形成 + result.append(nums[window.first!]) + } + + right += 1 + left += 1 + } + + return result +} +``` + -----------------------
From 40dc0d5e65fc36833c9d3fc77bba45606e83f739 Mon Sep 17 00:00:00 2001 From: bqlin Date: Mon, 20 Dec 2021 21:48:45 +0800 Subject: [PATCH 03/11] =?UTF-8?q?=E6=A0=88=E4=B8=8E=E9=98=9F=E5=88=97?= =?UTF-8?q?=E6=80=BB=E7=BB=93=EF=BC=9A=E4=BC=98=E5=8C=96=E6=8E=92=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/栈与队列总结.md | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/problems/栈与队列总结.md b/problems/栈与队列总结.md index 8ec96a29..15093ca7 100644 --- a/problems/栈与队列总结.md +++ b/problems/栈与队列总结.md @@ -158,22 +158,5 @@ cd a/b/c/../../ 好了,栈与队列我们就总结到这里了,接下来Carl就要带大家开启新的篇章了,大家加油! - - - -## 其他语言版本 - - -Java: - - -Python: - - -Go: - - - - -----------------------
From c88aee12e53a301ede5ed3fa81023d98c1ce009b Mon Sep 17 00:00:00 2001 From: bqlin Date: Tue, 21 Dec 2021 11:29:56 +0800 Subject: [PATCH 04/11] =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=A0=91=E7=90=86?= =?UTF-8?q?=E8=AE=BA=E5=9F=BA=E7=A1=80=EF=BC=9A=E4=BC=98=E5=8C=96=E6=8E=92?= =?UTF-8?q?=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/二叉树理论基础.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/problems/二叉树理论基础.md b/problems/二叉树理论基础.md index cc899850..c5dd118b 100644 --- a/problems/二叉树理论基础.md +++ b/problems/二叉树理论基础.md @@ -154,7 +154,7 @@ C++代码如下: -``` +```cpp struct TreeNode { int val; TreeNode *left; @@ -163,7 +163,7 @@ struct TreeNode { }; ``` -大家会发现二叉树的定义 和链表是差不多的,相对于链表 ,二叉树的节点里多了一个指针, 有两个指针,指向左右孩子. +大家会发现二叉树的定义 和链表是差不多的,相对于链表 ,二叉树的节点里多了一个指针, 有两个指针,指向左右孩子。 这里要提醒大家要注意二叉树节点定义的书写方式。 @@ -177,7 +177,7 @@ struct TreeNode { 本篇我们介绍了二叉树的种类、存储方式、遍历方式以及定义,比较全面的介绍了二叉树各个方面的重点,帮助大家扫一遍基础。 -**说道二叉树,就不得不说递归,很多同学对递归都是又熟悉又陌生,递归的代码一般很简短,但每次都是一看就会,一写就废。** +**说到二叉树,就不得不说递归,很多同学对递归都是又熟悉又陌生,递归的代码一般很简短,但每次都是一看就会,一写就废。** ## 其他语言版本 From f1a3fbc78851c01bb2b97cf004c85b3d15f41970 Mon Sep 17 00:00:00 2001 From: Anmizi <1845513904@qq.com> Date: Sun, 6 Feb 2022 23:33:11 +0800 Subject: [PATCH 05/11] =?UTF-8?q?Update=200110.=E5=B9=B3=E8=A1=A1=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改原有JS版本代码问题 --- problems/0110.平衡二叉树.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/problems/0110.平衡二叉树.md b/problems/0110.平衡二叉树.md index 9d43407a..313aeff2 100644 --- a/problems/0110.平衡二叉树.md +++ b/problems/0110.平衡二叉树.md @@ -614,8 +614,10 @@ var isBalanced = function(root) { if(node === null) return 0; // 3. 确定单层递归逻辑 let leftDepth = getDepth(node.left); //左子树高度 - let rightDepth = getDepth(node.right); //右子树高度 + // 当判定左子树不为平衡二叉树时,即可直接返回-1 if(leftDepth === -1) return -1; + let rightDepth = getDepth(node.right); //右子树高度 + // 当判定右子树不为平衡二叉树时,即可直接返回-1 if(rightDepth === -1) return -1; if(Math.abs(leftDepth - rightDepth) > 1) { return -1; From 8ce87963e5fe7e74657109d71c80ce3ce3b07da6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=81=E5=AE=A2=E5=AD=A6=E4=BC=9F?= Date: Mon, 7 Feb 2022 13:29:05 +0800 Subject: [PATCH 06/11] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=EF=BC=9A=E4=BB=A5=E4=B8=BA=E4=BD=BF=E7=94=A8=E4=BA=86?= =?UTF-8?q?=E9=80=92=E5=BD=92=EF=BC=8C=E5=85=B6=E5=AE=9E=E8=BF=98=E9=9A=90?= =?UTF-8?q?=E8=97=8F=E7=9D=80=E5=9B=9E=E6=BA=AF=20Swift=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/二叉树中递归带着回溯.md | 55 ++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/problems/二叉树中递归带着回溯.md b/problems/二叉树中递归带着回溯.md index 20b87f87..603854dc 100644 --- a/problems/二叉树中递归带着回溯.md +++ b/problems/二叉树中递归带着回溯.md @@ -515,6 +515,61 @@ var binaryTreePaths = function(root) { }; ``` +Swift: +> 100.相同的树 +```swift +// 递归 +func isSameTree(_ p: TreeNode?, _ q: TreeNode?) -> Bool { + return _isSameTree3(p, q) +} +func _isSameTree3(_ p: TreeNode?, _ q: TreeNode?) -> Bool { + if p == nil && q == nil { + return true + } else if p == nil && q != nil { + return false + } else if p != nil && q == nil { + return false + } else if p!.val != q!.val { + return false + } + let leftSide = _isSameTree3(p!.left, q!.left) + let rightSide = _isSameTree3(p!.right, q!.right) + return leftSide && rightSide +} +``` + +> 257.二叉树的不同路径 +```swift +// 递归/回溯 +func binaryTreePaths(_ root: TreeNode?) -> [String] { + var res = [String]() + guard let root = root else { + return res + } + var paths = [Int]() + _binaryTreePaths3(root, res: &res, paths: &paths) + return res +} +func _binaryTreePaths3(_ root: TreeNode, res: inout [String], paths: inout [Int]) { + paths.append(root.val) + if root.left == nil && root.right == nil { + var str = "" + for i in 0 ..< (paths.count - 1) { + str.append("\(paths[i])->") + } + str.append("\(paths.last!)") + res.append(str) + } + if let left = root.left { + _binaryTreePaths3(left, res: &res, paths: &paths) + paths.removeLast() + } + if let right = root.right { + _binaryTreePaths3(right, res: &res, paths: &paths) + paths.removeLast() + } +} +``` -----------------------
From 3aa54bfb8a8604a8cfb05a4fa8c606ebe6b09db3 Mon Sep 17 00:00:00 2001 From: Wayne <3522373084@qq.com> Date: Mon, 7 Feb 2022 21:39:51 +0800 Subject: [PATCH 07/11] =?UTF-8?q?343=E6=95=B4=E6=95=B0=E6=8B=86=E5=88=86,?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E7=AC=AC=E4=BA=8C=E5=B1=82=E5=BE=AA=E7=8E=AF?= =?UTF-8?q?=20j=20=E7=9A=84=E8=8C=83=E5=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0343.整数拆分.md | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/problems/0343.整数拆分.md b/problems/0343.整数拆分.md index 5d11f670..f616a606 100644 --- a/problems/0343.整数拆分.md +++ b/problems/0343.整数拆分.md @@ -197,14 +197,17 @@ Java: ```Java class Solution { public int integerBreak(int n) { - //dp[i]为正整数i拆分结果的最大乘积 - int[] dp = new int[n+1]; - dp[2] = 1; - for (int i = 3; i <= n; ++i) { - for (int j = 1; j < i - 1; ++j) { - //j*(i-j)代表把i拆分为j和i-j两个数相乘 - //j*dp[i-j]代表把i拆分成j和继续把(i-j)这个数拆分,取(i-j)拆分结果中的最大乘积与j相乘 - dp[i] = Math.max(dp[i], Math.max(j * (i - j), j * dp[i - j])); + //dp[i] 为正整数 i 拆分后的结果的最大乘积 + int[]dp=new int[n+1]; + dp[2]=1; + for(int i=3;i<=n;i++){ + for(int j=1;j<=i-j;j++){ + // 这里的 j 其实最大值为 i-j,再大只不过是重复而已, + //并且,在本题中,我们分析 dp[0], dp[1]都是无意义的, + //j 最大到 i-j,就不会用到 dp[0]与dp[1] + dp[i]=Math.max(dp[i],Math.max(j*(i-j),j*dp[i-j])); + // j * (i - j) 是单纯的把整数 i 拆分为两个数 也就是 i,i-j ,再相乘 + //而j * dp[i - j]是将 i 拆分成两个以及两个以上的个数,再相乘。 } } return dp[n]; From 1b7c86e20c2b36ed0732deb66a0c124a8e8fcdff Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Mon, 7 Feb 2022 23:01:47 +0800 Subject: [PATCH 08/11] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880257.=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E6=89=80=E6=9C=89=E8=B7=AF=E5=BE=84?= =?UTF-8?q?.md=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0257.二叉树的所有路径.md | 67 +++++++++++++++++++++-- 1 file changed, 61 insertions(+), 6 deletions(-) diff --git a/problems/0257.二叉树的所有路径.md b/problems/0257.二叉树的所有路径.md index 4078320f..1362897c 100644 --- a/problems/0257.二叉树的所有路径.md +++ b/problems/0257.二叉树的所有路径.md @@ -433,9 +433,9 @@ class Solution: if cur.right: self.traversal(cur.right, path + '->', result) ``` - + 迭代法: - + ```python3 from collections import deque @@ -463,13 +463,13 @@ class Solution: return result ``` - + --- Go: - + 递归法: - + ```go func binaryTreePaths(root *TreeNode) []string { res := make([]string, 0) @@ -492,7 +492,7 @@ func binaryTreePaths(root *TreeNode) []string { return res } ``` - + 迭代法: ```go @@ -581,7 +581,62 @@ var binaryTreePaths = function(root) { }; ``` +TypeScript: + +> 递归法 + +```typescript +function binaryTreePaths(root: TreeNode | null): string[] { + function recur(node: TreeNode, route: string, resArr: string[]): void { + route += String(node.val); + if (node.left === null && node.right === null) { + resArr.push(route); + return; + } + if (node.left !== null) recur(node.left, route + '->', resArr); + if (node.right !== null) recur(node.right, route + '->', resArr); + } + const resArr: string[] = []; + if (root === null) return resArr; + recur(root, '', resArr); + return resArr; +}; +``` + +> 迭代法 + +```typescript +// 迭代法2 +function binaryTreePaths(root: TreeNode | null): string[] { + let helperStack: TreeNode[] = []; + let tempNode: TreeNode; + let routeArr: string[] = []; + let resArr: string[] = []; + if (root !== null) { + helperStack.push(root); + routeArr.push(String(root.val)); + }; + while (helperStack.length > 0) { + tempNode = helperStack.pop()!; + let route: string = routeArr.pop()!; // tempNode 对应的路径 + if (tempNode.left === null && tempNode.right === null) { + resArr.push(route); + } + if (tempNode.right !== null) { + helperStack.push(tempNode.right); + routeArr.push(route + '->' + tempNode.right.val); // tempNode.right 对应的路径 + } + if (tempNode.left !== null) { + helperStack.push(tempNode.left); + routeArr.push(route + '->' + tempNode.left.val); // tempNode.left 对应的路径 + } + } + return resArr; +}; +``` + Swift: + > 递归/回溯 ```swift func binaryTreePaths(_ root: TreeNode?) -> [String] { From 47818c947888ecff3dae6f36daded1cfaf92db3b Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Tue, 8 Feb 2022 10:23:59 +0800 Subject: [PATCH 09/11] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=88=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E4=B8=AD=E9=80=92=E5=BD=92=E5=B8=A6=E7=9D=80?= =?UTF-8?q?=E5=9B=9E=E6=BA=AF.md=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E7=9B=B8=E5=90=8C=E7=9A=84=E6=A0=91typescript=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/二叉树中递归带着回溯.md | 23 ++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/problems/二叉树中递归带着回溯.md b/problems/二叉树中递归带着回溯.md index 20b87f87..03815ed3 100644 --- a/problems/二叉树中递归带着回溯.md +++ b/problems/二叉树中递归带着回溯.md @@ -515,6 +515,29 @@ var binaryTreePaths = function(root) { }; ``` +TypeScript: + +> 相同的树 + +```typescript +function isSameTree(p: TreeNode | null, q: TreeNode | null): boolean { + if (p === null && q === null) return true; + if (p === null || q === null) return false; + if (p.val !== q.val) return false; + let bool1: boolean, bool2: boolean; + bool1 = isSameTree(p.left, q.left); + bool2 = isSameTree(p.right, q.right); + return bool1 && bool2; +}; +``` + +> 二叉树的不同路径 + +```typescript +``` + + + -----------------------
From 49a1a42bbed494b10b4d6659ba787830f62bd842 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Tue, 8 Feb 2022 10:45:15 +0800 Subject: [PATCH 10/11] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=88=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E4=B8=AD=E9=80=92=E5=BD=92=E5=B8=A6=E7=9D=80?= =?UTF-8?q?=E5=9B=9E=E6=BA=AF.md=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=A0=91=E7=9A=84=E4=B8=8D=E5=90=8C=E8=B7=AF?= =?UTF-8?q?=E5=BE=84=E7=9A=84typescript=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/二叉树中递归带着回溯.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/problems/二叉树中递归带着回溯.md b/problems/二叉树中递归带着回溯.md index 03815ed3..41d5663e 100644 --- a/problems/二叉树中递归带着回溯.md +++ b/problems/二叉树中递归带着回溯.md @@ -534,6 +534,27 @@ function isSameTree(p: TreeNode | null, q: TreeNode | null): boolean { > 二叉树的不同路径 ```typescript +function binaryTreePaths(root: TreeNode | null): string[] { + function recur(node: TreeNode, nodeSeqArr: number[], resArr: string[]): void { + nodeSeqArr.push(node.val); + if (node.left === null && node.right === null) { + resArr.push(nodeSeqArr.join('->')); + } + if (node.left !== null) { + recur(node.left, nodeSeqArr, resArr); + nodeSeqArr.pop(); + } + if (node.right !== null) { + recur(node.right, nodeSeqArr, resArr); + nodeSeqArr.pop(); + } + } + let nodeSeqArr: number[] = []; + let resArr: string[] = []; + if (root === null) return resArr; + recur(root, nodeSeqArr, resArr); + return resArr; +}; ``` From f604aea34c6632e20ad4ced37cb24c005420e574 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Tue, 8 Feb 2022 23:00:31 +0800 Subject: [PATCH 11/11] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880404.=E5=B7=A6?= =?UTF-8?q?=E5=8F=B6=E5=AD=90=E4=B9=8B=E5=92=8C.md=EF=BC=89=EF=BC=9A?= =?UTF-8?q?=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/0404.左叶子之和.md | 44 ++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/problems/0404.左叶子之和.md b/problems/0404.左叶子之和.md index 09272052..2b4df151 100644 --- a/problems/0404.左叶子之和.md +++ b/problems/0404.左叶子之和.md @@ -372,6 +372,50 @@ var sumOfLeftLeaves = function(root) { }; ``` +## TypeScript + +> 递归法 + +```typescript +function sumOfLeftLeaves(root: TreeNode | null): number { + if (root === null) return 0; + let midVal: number = 0; + if ( + root.left !== null && + root.left.left === null && + root.left.right === null + ) { + midVal = root.left.val; + } + let leftVal: number = sumOfLeftLeaves(root.left); + let rightVal: number = sumOfLeftLeaves(root.right); + return midVal + leftVal + rightVal; +}; +``` + +> 迭代法 + +```typescript +function sumOfLeftLeaves(root: TreeNode | null): number { + let helperStack: TreeNode[] = []; + let tempNode: TreeNode; + let sum: number = 0; + if (root !== null) helperStack.push(root); + while (helperStack.length > 0) { + tempNode = helperStack.pop()!; + if ( + tempNode.left !== null && + tempNode.left.left === null && + tempNode.left.right === null + ) { + sum += tempNode.left.val; + } + if (tempNode.right !== null) helperStack.push(tempNode.right); + if (tempNode.left !== null) helperStack.push(tempNode.left); + } + return sum; +}; +``` ## Swift