diff --git a/problems/0110.平衡二叉树.md b/problems/0110.平衡二叉树.md index d9dcf289..411434d4 100644 --- a/problems/0110.平衡二叉树.md +++ b/problems/0110.平衡二叉树.md @@ -615,8 +615,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; diff --git a/problems/0239.滑动窗口最大值.md b/problems/0239.滑动窗口最大值.md index adf3548c..ce215a7e 100644 --- a/problems/0239.滑动窗口最大值.md +++ b/problems/0239.滑动窗口最大值.md @@ -45,7 +45,7 @@ 这个队列应该长这个样子: -``` +```cpp class MyQueue { public: void pop(int value) { @@ -597,5 +597,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 +} +``` + -----------------------
diff --git a/problems/0257.二叉树的所有路径.md b/problems/0257.二叉树的所有路径.md index a0c718f4..1362897c 100644 --- a/problems/0257.二叉树的所有路径.md +++ b/problems/0257.二叉树的所有路径.md @@ -433,10 +433,10 @@ class Solution: if cur.right: self.traversal(cur.right, path + '->', result) ``` - + 迭代法: - -```python + +```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] { diff --git a/problems/0343.整数拆分.md b/problems/0343.整数拆分.md index 282670a3..b6182ed6 100644 --- a/problems/0343.整数拆分.md +++ b/problems/0343.整数拆分.md @@ -196,14 +196,17 @@ public: ```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]; diff --git a/problems/0404.左叶子之和.md b/problems/0404.左叶子之和.md index 8c6eaddb..d29691a5 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 diff --git a/problems/1047.删除字符串中的所有相邻重复项.md b/problems/1047.删除字符串中的所有相邻重复项.md index b94e557d..54455f62 100644 --- a/problems/1047.删除字符串中的所有相邻重复项.md +++ b/problems/1047.删除字符串中的所有相邻重复项.md @@ -346,14 +346,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) diff --git a/problems/二叉树中递归带着回溯.md b/problems/二叉树中递归带着回溯.md index 20b87f87..1e9f9cbf 100644 --- a/problems/二叉树中递归带着回溯.md +++ b/problems/二叉树中递归带着回溯.md @@ -171,7 +171,7 @@ if (cur->right) { ## 其他语言版本 -Java: +### Java: 100. 相同的树:递归代码 ```java class Solution { @@ -252,7 +252,7 @@ Java: } ``` -Python: +### Python: 100.相同的树 > 递归法 @@ -332,7 +332,7 @@ class Solution: self.traversal(cur.right, path+"->", result) #右 回溯就隐藏在这里 ``` -Go: +### Go: 100.相同的树 ```go @@ -436,7 +436,7 @@ func traversal(root *TreeNode,result *[]string,path *[]int){ } ``` -JavaScript: +### JavaScript: 100.相同的树 ```javascript @@ -516,5 +516,107 @@ 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 +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; +}; +``` + + + + +### 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() + } +} +``` + + -----------------------
diff --git a/problems/二叉树理论基础.md b/problems/二叉树理论基础.md index 009e8276..62e3b19a 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 { 本篇我们介绍了二叉树的种类、存储方式、遍历方式以及定义,比较全面的介绍了二叉树各个方面的重点,帮助大家扫一遍基础。 -**说道二叉树,就不得不说递归,很多同学对递归都是又熟悉又陌生,递归的代码一般很简短,但每次都是一看就会,一写就废。** +**说到二叉树,就不得不说递归,很多同学对递归都是又熟悉又陌生,递归的代码一般很简短,但每次都是一看就会,一写就废。** ## 其他语言版本 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: - - - - -----------------------