From 1efb77e96310d884eb2b8647232dd1586f4f0afb Mon Sep 17 00:00:00 2001 From: zucong Date: Tue, 25 Jan 2022 14:33:12 +0800 Subject: [PATCH 01/32] =?UTF-8?q?=E4=BC=98=E5=8C=96=200112.=E8=B7=AF?= =?UTF-8?q?=E5=BE=84=E6=80=BB=E5=92=8C=20Go=E7=89=88=E6=9C=AC=E8=A7=A3?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0112.路径总和.md | 101 ++++++++++++++-------------------- 1 file changed, 41 insertions(+), 60 deletions(-) diff --git a/problems/0112.路径总和.md b/problems/0112.路径总和.md index 910e57c8..b3e121ba 100644 --- a/problems/0112.路径总和.md +++ b/problems/0112.路径总和.md @@ -531,82 +531,63 @@ class solution: ```go //递归法 /** - * definition for a binary tree node. - * type treenode struct { - * val int - * left *treenode - * right *treenode + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode * } */ -func haspathsum(root *treenode, targetsum int) bool { - var flage bool //找没找到的标志 - if root==nil{ - return flage +func hasPathSum(root *TreeNode, targetSum int) bool { + if root == nil { + return false } - pathsum(root,0,targetsum,&flage) - return flage -} -func pathsum(root *treenode, sum int,targetsum int,flage *bool){ - sum+=root.val - if root.left==nil&&root.right==nil&&sum==targetsum{ - *flage=true - return - } - if root.left!=nil&&!(*flage){//左节点不为空且还没找到 - pathsum(root.left,sum,targetsum,flage) - } - if root.right!=nil&&!(*flage){//右节点不为空且没找到 - pathsum(root.right,sum,targetsum,flage) + + targetSum -= root.Val // 将targetSum在遍历每层的时候都减去本层节点的值 + if root.Left == nil && root.Right == nil && targetSum == 0 { // 如果剩余的targetSum为0, 则正好就是符合的结果 + return true } + return hasPathSum(root.Left, targetSum) || hasPathSum(root.Right, targetSum) // 否则递归找 } ``` -113 递归法 +113. 路径总和 II ```go /** - * definition for a binary tree node. - * type treenode struct { - * val int - * left *treenode - * right *treenode + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode * } */ -func pathsum(root *treenode, targetsum int) [][]int { - var result [][]int//最终结果 - if root==nil{ - return result - } - var sumnodes []int//经过路径的节点集合 - haspathsum(root,&sumnodes,targetsum,&result) +func pathSum(root *TreeNode, targetSum int) [][]int { + result := make([][]int, 0) + traverse(root, &result, new([]int), targetSum) return result } -func haspathsum(root *treenode,sumnodes *[]int,targetsum int,result *[][]int){ - *sumnodes=append(*sumnodes,root.val) - if root.left==nil&&root.right==nil{//叶子节点 - fmt.println(*sumnodes) - var sum int - var number int - for k,v:=range *sumnodes{//求该路径节点的和 - sum+=v - number=k - } - tempnodes:=make([]int,number+1)//新的nodes接受指针里的值,防止最终指针里的值发生变动,导致最后的结果都是最后一个sumnodes的值 - for k,v:=range *sumnodes{ - tempnodes[k]=v - } - if sum==targetsum{ - *result=append(*result,tempnodes) - } + +func traverse(node *TreeNode, result *[][]int, currPath *[]int, targetSum int) { + if node == nil { // 这个判空也可以挪到递归遍历左右子树时去判断 + return } - if root.left!=nil{ - haspathsum(root.left,sumnodes,targetsum,result) - *sumnodes=(*sumnodes)[:len(*sumnodes)-1]//回溯 - } - if root.right!=nil{ - haspathsum(root.right,sumnodes,targetsum,result) - *sumnodes=(*sumnodes)[:len(*sumnodes)-1]//回溯 + + targetSum -= node.Val // 将targetSum在遍历每层的时候都减去本层节点的值 + *currPath = append(*currPath, node.Val) // 把当前节点放到路径记录里 + + if node.Left == nil && node.Right == nil && targetSum == 0 { // 如果剩余的targetSum为0, 则正好就是符合的结果 + // 不能直接将currPath放到result里面, 因为currPath是共享的, 每次遍历子树时都会被修改 + pathCopy := make([]int, len(*currPath)) + for i, element := range *currPath { + pathCopy[i] = element + } + *result = append(*result, pathCopy) // 将副本放到结果集里 } + + traverse(node.Left, result, currPath, targetSum) + traverse(node.Right, result, currPath, targetSum) + *currPath = (*currPath)[:len(*currPath)-1] // 当前节点遍历完成, 从路径记录里删除掉 } ``` From 7ed2a3aef7b10b48fe7f8127f185f4a6efc9952c Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Wed, 26 Jan 2022 19:18:23 +0800 Subject: [PATCH 02/32] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=88=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E7=BB=9F=E4=B8=80=E8=BF=AD=E4=BB=A3?= =?UTF-8?q?=E6=B3=95.md=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/二叉树的统一迭代法.md | 68 +++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/problems/二叉树的统一迭代法.md b/problems/二叉树的统一迭代法.md index 2ff84817..f6edf586 100644 --- a/problems/二叉树的统一迭代法.md +++ b/problems/二叉树的统一迭代法.md @@ -522,7 +522,75 @@ var postorderTraversal = function(root, res = []) { ``` +TypeScript: +```typescript +// 前序遍历(迭代法) +function preorderTraversal(root: TreeNode | null): number[] { + let helperStack: (TreeNode | null)[] = []; + let res: number[] = []; + let curNode: TreeNode | null; + if (root === null) return res; + helperStack.push(root); + while (helperStack.length > 0) { + curNode = helperStack.pop()!; + if (curNode !== null) { + if (curNode.right !== null) helperStack.push(curNode.right); + helperStack.push(curNode); + helperStack.push(null); + if (curNode.left !== null) helperStack.push(curNode.left); + } else { + curNode = helperStack.pop()!; + res.push(curNode.val); + } + } + return res; +}; + +// 中序遍历(迭代法) +function inorderTraversal(root: TreeNode | null): number[] { + let helperStack: (TreeNode | null)[] = []; + let res: number[] = []; + let curNode: TreeNode | null; + if (root === null) return res; + helperStack.push(root); + while (helperStack.length > 0) { + curNode = helperStack.pop()!; + if (curNode !== null) { + if (curNode.right !== null) helperStack.push(curNode.right); + helperStack.push(curNode); + helperStack.push(null); + if (curNode.left !== null) helperStack.push(curNode.left); + } else { + curNode = helperStack.pop()!; + res.push(curNode.val); + } + } + return res; +}; + +// 后序遍历(迭代法) +function postorderTraversal(root: TreeNode | null): number[] { + let helperStack: (TreeNode | null)[] = []; + let res: number[] = []; + let curNode: TreeNode | null; + if (root === null) return res; + helperStack.push(root); + while (helperStack.length > 0) { + curNode = helperStack.pop()!; + if (curNode !== null) { + if (curNode.right !== null) helperStack.push(curNode.right); + helperStack.push(curNode); + helperStack.push(null); + if (curNode.left !== null) helperStack.push(curNode.left); + } else { + curNode = helperStack.pop()!; + res.push(curNode.val); + } + } + return res; +}; +``` -----------------------
From 878231551829fdd9deea2f2457eb09b794d7ed65 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Wed, 26 Jan 2022 21:20:36 +0800 Subject: [PATCH 03/32] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880102.=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E5=B1=82=E5=BA=8F=E9=81=8D=E5=8E=86?= =?UTF-8?q?.md=20&=20107.=E4=BA=8C=E5=8F=89=E6=A0=91=E7=9A=84=E5=B1=82?= =?UTF-8?q?=E6=AC=A1=E9=81=8D=E5=8E=86=20II=EF=BC=89=EF=BC=9A=E5=A2=9E?= =?UTF-8?q?=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/0102.二叉树的层序遍历.md | 52 +++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md index 1fb9b633..b108c8f0 100644 --- a/problems/0102.二叉树的层序遍历.md +++ b/problems/0102.二叉树的层序遍历.md @@ -246,7 +246,35 @@ var levelOrder = function(root) { ``` +TypeScript: + +```typescript +function levelOrder(root: TreeNode | null): number[][] { + let helperQueue: TreeNode[] = []; + let res: number[][] = []; + let tempArr: number[] = []; + if (root !== null) helperQueue.push(root); + let curNode: TreeNode; + while (helperQueue.length > 0) { + for (let i = 0, length = helperQueue.length; i < length; i++) { + curNode = helperQueue.shift()!; + tempArr.push(curNode.val); + if (curNode.left !== null) { + helperQueue.push(curNode.left); + } + if (curNode.right !== null) { + helperQueue.push(curNode.right); + } + } + res.push(tempArr); + tempArr = []; + } + return res; +}; +``` + Swift: + ```swift func levelOrder(_ root: TreeNode?) -> [[Int]] { var res = [[Int]]() @@ -454,7 +482,31 @@ var levelOrderBottom = function(root) { }; ``` +TypeScript: + +```typescript +function levelOrderBottom(root: TreeNode | null): number[][] { + let helperQueue: TreeNode[] = []; + let resArr: number[][] = []; + let tempArr: number[] = []; + let tempNode: TreeNode; + if (root !== null) helperQueue.push(root); + while (helperQueue.length > 0) { + for (let i = 0, length = helperQueue.length; i < length; i++) { + tempNode = helperQueue.shift()!; + tempArr.push(tempNode.val); + if (tempNode.left !== null) helperQueue.push(tempNode.left); + if (tempNode.right !== null) helperQueue.push(tempNode.right); + } + resArr.push(tempArr); + tempArr = []; + } + return resArr.reverse(); +}; +``` + Swift: + ```swift func levelOrderBottom(_ root: TreeNode?) -> [[Int]] { var res = [[Int]]() From e177bca526cd33ff4dce70b188f2214598941b0e Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Thu, 27 Jan 2022 22:35:30 +0800 Subject: [PATCH 04/32] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=88199.=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E5=8F=B3=E8=A7=86=E5=9B=BE=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/0102.二叉树的层序遍历.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md index b108c8f0..18e19227 100644 --- a/problems/0102.二叉树的层序遍历.md +++ b/problems/0102.二叉树的层序遍历.md @@ -709,7 +709,28 @@ var rightSideView = function(root) { }; ``` +TypeScript: + +```typescript +function rightSideView(root: TreeNode | null): number[] { + let helperQueue: TreeNode[] = []; + let resArr: number[] = []; + let tempNode: TreeNode; + if (root !== null) helperQueue.push(root); + while (helperQueue.length > 0) { + for (let i = 0, length = helperQueue.length; i < length; i++) { + tempNode = helperQueue.shift()!; + if (i === length - 1) resArr.push(tempNode.val); + if (tempNode.left !== null) helperQueue.push(tempNode.left); + if (tempNode.right !== null) helperQueue.push(tempNode.right); + } + } + return resArr; +}; +``` + Swift: + ```swift func rightSideView(_ root: TreeNode?) -> [Int] { var res = [Int]() From daf710bde6eb82f2c5b2a87254ca9860d79af80e Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Fri, 28 Jan 2022 11:32:57 +0800 Subject: [PATCH 05/32] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=88637.=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E5=B1=82=E5=B9=B3=E5=9D=87=E5=80=BC?= =?UTF-8?q?=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/0102.二叉树的层序遍历.md | 25 +++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md index 18e19227..9ff4f2e9 100644 --- a/problems/0102.二叉树的层序遍历.md +++ b/problems/0102.二叉树的层序遍历.md @@ -941,7 +941,32 @@ var averageOfLevels = function(root) { }; ``` +TypeScript: + +```typescript +function averageOfLevels(root: TreeNode | null): number[] { + let helperQueue: TreeNode[] = []; + let resArr: number[] = []; + let total: number = 0; + let tempNode: TreeNode; + if (root !== null) helperQueue.push(root); + while (helperQueue.length > 0) { + let length = helperQueue.length; + for (let i = 0; i < length; i++) { + tempNode = helperQueue.shift()!; + total += tempNode.val; + if (tempNode.left) helperQueue.push(tempNode.left); + if (tempNode.right) helperQueue.push(tempNode.right); + } + resArr.push(total / length); + total = 0; + } + return resArr; +}; +``` + Swift: + ```swift func averageOfLevels(_ root: TreeNode?) -> [Double] { var res = [Double]() From 2862d47368512a9cbc7e7184775d99d8e53f72df Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Fri, 28 Jan 2022 11:44:49 +0800 Subject: [PATCH 06/32] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=88429.N=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E5=B1=82=E5=BA=8F=E9=81=8D=E5=8E=86=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/0102.二叉树的层序遍历.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md index 9ff4f2e9..5dd448b7 100644 --- a/problems/0102.二叉树的层序遍历.md +++ b/problems/0102.二叉树的层序遍历.md @@ -1190,7 +1190,30 @@ var levelOrder = function(root) { }; ``` +TypeScript: + +```typescript +function levelOrder(root: Node | null): number[][] { + let helperQueue: Node[] = []; + let resArr: number[][] = []; + let tempArr: number[] = []; + if (root !== null) helperQueue.push(root); + let curNode: Node; + while (helperQueue.length > 0) { + for (let i = 0, length = helperQueue.length; i < length; i++) { + curNode = helperQueue.shift()!; + tempArr.push(curNode.val); + helperQueue.push(...curNode.children); + } + resArr.push(tempArr); + tempArr = []; + } + return resArr; +}; +``` + Swift: + ```swift func levelOrder(_ root: Node?) -> [[Int]] { var res = [[Int]]() From 9329ecff5e95e5bc1e8cb17fbba1135765fd3286 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Fri, 28 Jan 2022 12:02:15 +0800 Subject: [PATCH 07/32] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=88515.=E5=9C=A8?= =?UTF-8?q?=E6=AF=8F=E4=B8=AA=E6=A0=91=E8=A1=8C=E4=B8=AD=E6=89=BE=E6=9C=80?= =?UTF-8?q?=E5=A4=A7=E5=80=BC=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typescrip?= =?UTF-8?q?t=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0102.二叉树的层序遍历.md | 27 +++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md index 5dd448b7..6f90cb75 100644 --- a/problems/0102.二叉树的层序遍历.md +++ b/problems/0102.二叉树的层序遍历.md @@ -1393,7 +1393,34 @@ var largestValues = function(root) { }; ``` +TypeScript: + +```typescript +function largestValues(root: TreeNode | null): number[] { + let helperQueue: TreeNode[] = []; + let resArr: number[] = []; + let tempNode: TreeNode; + let max: number = 0; + if (root !== null) helperQueue.push(root); + while (helperQueue.length > 0) { + for (let i = 0, length = helperQueue.length; i < length; i++) { + tempNode = helperQueue.shift()!; + if (i === 0) { + max = tempNode.val; + } else { + max = max > tempNode.val ? max : tempNode.val; + } + if (tempNode.left) helperQueue.push(tempNode.left); + if (tempNode.right) helperQueue.push(tempNode.right); + } + resArr.push(max); + } + return resArr; +}; +``` + Swift: + ```swift func largestValues(_ root: TreeNode?) -> [Int] { var res = [Int]() From 992cd6d7f3bc2b0065bf8f3f3ff064a13c66dde3 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Fri, 28 Jan 2022 16:10:56 +0800 Subject: [PATCH 08/32] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=88116.=E5=A1=AB?= =?UTF-8?q?=E5=85=85=E6=AF=8F=E4=B8=AA=E8=8A=82=E7=82=B9=E7=9A=84=E4=B8=8B?= =?UTF-8?q?=E4=B8=80=E4=B8=AA=E5=8F=B3=E4=BE=A7=E8=8A=82=E7=82=B9=E6=8C=87?= =?UTF-8?q?=E9=92=88=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0102.二叉树的层序遍历.md | 25 +++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md index 6f90cb75..12f96469 100644 --- a/problems/0102.二叉树的层序遍历.md +++ b/problems/0102.二叉树的层序遍历.md @@ -1611,6 +1611,31 @@ var connect = function(root) { }; ``` +TypeScript: + +```typescript +function connect(root: Node | null): Node | null { + let helperQueue: Node[] = []; + let preNode: Node, curNode: Node; + if (root !== null) helperQueue.push(root); + while (helperQueue.length > 0) { + for (let i = 0, length = helperQueue.length; i < length; i++) { + if (i === 0) { + preNode = helperQueue.shift()!; + } else { + curNode = helperQueue.shift()!; + preNode.next = curNode; + preNode = curNode; + } + if (preNode.left) helperQueue.push(preNode.left); + if (preNode.right) helperQueue.push(preNode.right); + } + preNode.next = null; + } + return root; +}; +``` + go: ```GO From d1abb0cbb4397a950479788497e3ec21adbcea22 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Fri, 28 Jan 2022 16:21:25 +0800 Subject: [PATCH 09/32] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=88117.=E5=A1=AB?= =?UTF-8?q?=E5=85=85=E6=AF=8F=E4=B8=AA=E8=8A=82=E7=82=B9=E7=9A=84=E4=B8=8B?= =?UTF-8?q?=E4=B8=80=E4=B8=AA=E5=8F=B3=E4=BE=A7=E8=8A=82=E7=82=B9=E6=8C=87?= =?UTF-8?q?=E9=92=88II=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0102.二叉树的层序遍历.md | 25 +++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md index 12f96469..a7716d92 100644 --- a/problems/0102.二叉树的层序遍历.md +++ b/problems/0102.二叉树的层序遍历.md @@ -1862,6 +1862,31 @@ var connect = function(root) { return root; }; ``` +TypeScript: + +```typescript +function connect(root: Node | null): Node | null { + let helperQueue: Node[] = []; + let preNode: Node, curNode: Node; + if (root !== null) helperQueue.push(root); + while (helperQueue.length > 0) { + for (let i = 0, length = helperQueue.length; i < length; i++) { + if (i === 0) { + preNode = helperQueue.shift()!; + } else { + curNode = helperQueue.shift()!; + preNode.next = curNode; + preNode = curNode; + } + if (preNode.left) helperQueue.push(preNode.left); + if (preNode.right) helperQueue.push(preNode.right); + } + preNode.next = null; + } + return root; +}; +``` + go: ```GO From bdb0954e32976832b9bd99dd2dcb2744d55e665b Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Fri, 28 Jan 2022 17:11:11 +0800 Subject: [PATCH 10/32] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=88104.=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E6=9C=80=E5=A4=A7=E6=B7=B1=E5=BA=A6?= =?UTF-8?q?=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/0102.二叉树的层序遍历.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md index a7716d92..9ac23fdf 100644 --- a/problems/0102.二叉树的层序遍历.md +++ b/problems/0102.二叉树的层序遍历.md @@ -2131,7 +2131,28 @@ var maxDepth = function(root) { }; ``` +TypeScript: + +```typescript +function maxDepth(root: TreeNode | null): number { + let helperQueue: TreeNode[] = []; + let resDepth: number = 0; + let tempNode: TreeNode; + if (root !== null) helperQueue.push(root); + while (helperQueue.length > 0) { + resDepth++; + for (let i = 0, length = helperQueue.length; i < length; i++) { + tempNode = helperQueue.shift()!; + if (tempNode.left) helperQueue.push(tempNode.left); + if (tempNode.right) helperQueue.push(tempNode.right); + } + } + return resDepth; +}; +``` + Swift: + ```swift func maxDepth(_ root: TreeNode?) -> Int { guard let root = root else { From 626db3fc67ba0a478f39331c4788bca15314a720 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Fri, 28 Jan 2022 20:33:04 +0800 Subject: [PATCH 11/32] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=88111.=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E6=9C=80=E5=B0=8F=E6=B7=B1=E5=BA=A6?= =?UTF-8?q?=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/0102.二叉树的层序遍历.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md index 9ac23fdf..1a92d42f 100644 --- a/problems/0102.二叉树的层序遍历.md +++ b/problems/0102.二叉树的层序遍历.md @@ -2349,7 +2349,29 @@ var minDepth = function(root) { }; ``` +TypeScript: + +```typescript +function minDepth(root: TreeNode | null): number { + let helperQueue: TreeNode[] = []; + let resMin: number = 0; + let tempNode: TreeNode; + if (root !== null) helperQueue.push(root); + while (helperQueue.length > 0) { + resMin++; + for (let i = 0, length = helperQueue.length; i < length; i++) { + tempNode = helperQueue.shift()!; + if (tempNode.left === null && tempNode.right === null) return resMin; + if (tempNode.left !== null) helperQueue.push(tempNode.left); + if (tempNode.right !== null) helperQueue.push(tempNode.right); + } + } + return resMin; +}; +``` + Swift: + ```swift func minDepth(_ root: TreeNode?) -> Int { guard let root = root else { From 653d645dabd54c9b2d94afc27dc3cb308f82b519 Mon Sep 17 00:00:00 2001 From: Younglesszzz <571688981@qq.com> Date: Sat, 29 Jan 2022 13:29:28 +0800 Subject: [PATCH 12/32] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86=E4=BA=8C?= =?UTF-8?q?=E7=BB=B4=E6=95=B0=E7=BB=84=E7=89=88=E6=9C=AC=EF=BC=8C=E7=9B=B8?= =?UTF-8?q?=E6=AF=94=E4=BA=8E=E4=B8=80=E7=BB=B4=E5=A5=BD=E7=90=86=E8=A7=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../1049.最后一块石头的重量II.md | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/problems/1049.最后一块石头的重量II.md b/problems/1049.最后一块石头的重量II.md index d64e7e56..7b67b1ac 100644 --- a/problems/1049.最后一块石头的重量II.md +++ b/problems/1049.最后一块石头的重量II.md @@ -153,6 +153,8 @@ public: Java: + +一维数组版本 ```Java class Solution { public int lastStoneWeightII(int[] stones) { @@ -173,6 +175,41 @@ class Solution { return sum - 2 * dp[target]; } } +``` +二维数组版本(便于理解) +```Java +class Solution { + public int lastStoneWeightII(int[] stones) { + int sum = 0; + for (int s : stones) { + sum += s; + } + + int target = sum / 2; + //初始化,dp[i][j]为可以放0-i物品,背包容量为j的情况下背包中的最大价值 + int[][] dp = new int[stones.length][target + 1]; + //dp[i][0]默认初始化为0 + //dp[0][j]取决于stones[0] + for (int j = stones[0]; j <= target; j++) { + dp[0][j] = stones[0]; + } + + for (int i = 1; i < stones.length; i++) { + for (int j = 1; j <= target; j++) {//注意是等于 + if (j >= stones[i]) { + //不放:dp[i - 1][j] 放:dp[i - 1][j - stones[i]] + stones[i] + dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - stones[i]] + stones[i]); + } else { + dp[i][j] = dp[i - 1][j]; + } + } + } + + System.out.println(dp[stones.length - 1][target]); + return (sum - dp[stones.length - 1][target]) - dp[stones.length - 1][target]; + } +} + ``` Python: From b1c3d5c4f9db71676ebc44f9c41a10778dfea1c5 Mon Sep 17 00:00:00 2001 From: Younglesszzz <571688981@qq.com> Date: Sat, 29 Jan 2022 13:39:27 +0800 Subject: [PATCH 13/32] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86=E4=BA=8C?= =?UTF-8?q?=E7=BB=B4=E6=95=B0=E7=BB=84=E7=89=88=E6=9C=AC=EF=BC=8C=E7=9B=B8?= =?UTF-8?q?=E6=AF=94=E4=BA=8E=E4=B8=80=E7=BB=B4=E5=A5=BD=E7=90=86=E8=A7=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0416.分割等和子集.md | 39 +++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/problems/0416.分割等和子集.md b/problems/0416.分割等和子集.md index 45b8b416..9da1f8d5 100644 --- a/problems/0416.分割等和子集.md +++ b/problems/0416.分割等和子集.md @@ -210,6 +210,45 @@ class Solution { } ``` +二维数组版本(易于理解): +```Java +class Solution { + public boolean canPartition(int[] nums) { + int sum = 0; + for (int i = 0; i < nums.length; i++) { + sum += nums[i]; + } + + if (sum % 2 == 1) + return false; + int target = sum / 2; + + //dp[i][j]代表可装物品为0-i,背包容量为j的情况下,背包内容量的最大价值 + int[][] dp = new int[nums.length][target + 1]; + + //初始化,dp[0][j]的最大价值nums[0](if j > weight[i]) + //dp[i][0]均为0,不用初始化 + for (int j = nums[0]; j <= target; j++) { + dp[0][j] = nums[0]; + } + + //遍历物品,遍历背包 + //递推公式: + for (int i = 1; i < nums.length; i++) { + for (int j = 0; j <= target; j++) { + //背包容量可以容纳nums[i] + if (j >= nums[i]) { + dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - nums[i]] + nums[i]); + } else { + dp[i][j] = dp[i - 1][j]; + } + } + } + + return dp[nums.length - 1][target] == target; + } +} +``` Python: ```python class Solution: From be3f2cfc8a6e0abc3fbbefb3d7d2967b686e96e2 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Sat, 29 Jan 2022 22:12:49 +0800 Subject: [PATCH 14/32] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880226.=E7=BF=BB?= =?UTF-8?q?=E8=BD=AC=E4=BA=8C=E5=8F=89=E6=A0=91.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/0226.翻转二叉树.md | 128 +++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) diff --git a/problems/0226.翻转二叉树.md b/problems/0226.翻转二叉树.md index e6dbb709..8108e7ad 100644 --- a/problems/0226.翻转二叉树.md +++ b/problems/0226.翻转二叉树.md @@ -563,7 +563,135 @@ var invertTree = function(root) { }; ``` +### TypeScript: + +递归法: + +```typescript +// 递归法(前序遍历) +function invertTree(root: TreeNode | null): TreeNode | null { + if (root === null) return root; + let tempNode: TreeNode | null = root.left; + root.left = root.right; + root.right = tempNode; + invertTree(root.left); + invertTree(root.right); + return root; +}; + +// 递归法(后序遍历) +function invertTree(root: TreeNode | null): TreeNode | null { + if (root === null) return root; + invertTree(root.left); + invertTree(root.right); + let tempNode: TreeNode | null = root.left; + root.left = root.right; + root.right = tempNode; + return root; +}; + +// 递归法(中序遍历) +function invertTree(root: TreeNode | null): TreeNode | null { + if (root === null) return root; + invertTree(root.left); + let tempNode: TreeNode | null = root.left; + root.left = root.right; + root.right = tempNode; + // 因为左右节点已经进行交换,此时的root.left 是原先的root.right + invertTree(root.left); + return root; +}; +``` + +迭代法: + +```typescript +// 迭代法(栈模拟前序遍历) +function invertTree(root: TreeNode | null): TreeNode | null { + let helperStack: TreeNode[] = []; + let curNode: TreeNode, + tempNode: TreeNode | null; + if (root !== null) helperStack.push(root); + while (helperStack.length > 0) { + curNode = helperStack.pop()!; + // 入栈操作最好在交换节点之前进行,便于理解 + if (curNode.right) helperStack.push(curNode.right); + if (curNode.left) helperStack.push(curNode.left); + tempNode = curNode.left; + curNode.left = curNode.right; + curNode.right = tempNode; + } + return root; +}; + +// 迭代法(栈模拟中序遍历-统一写法形式) +function invertTree(root: TreeNode | null): TreeNode | null { + let helperStack: (TreeNode | null)[] = []; + let curNode: TreeNode | null, + tempNode: TreeNode | null; + if (root !== null) helperStack.push(root); + while (helperStack.length > 0) { + curNode = helperStack.pop(); + if (curNode !== null) { + if (curNode.right !== null) helperStack.push(curNode.right); + helperStack.push(curNode); + helperStack.push(null); + if (curNode.left !== null) helperStack.push(curNode.left); + } else { + curNode = helperStack.pop()!; + tempNode = curNode.left; + curNode.left = curNode.right; + curNode.right = tempNode; + } + } + return root; +}; + +// 迭代法(栈模拟后序遍历-统一写法形式) +function invertTree(root: TreeNode | null): TreeNode | null { + let helperStack: (TreeNode | null)[] = []; + let curNode: TreeNode | null, + tempNode: TreeNode | null; + if (root !== null) helperStack.push(root); + while (helperStack.length > 0) { + curNode = helperStack.pop(); + if (curNode !== null) { + helperStack.push(curNode); + helperStack.push(null); + if (curNode.right !== null) helperStack.push(curNode.right); + if (curNode.left !== null) helperStack.push(curNode.left); + } else { + curNode = helperStack.pop()!; + tempNode = curNode.left; + curNode.left = curNode.right; + curNode.right = tempNode; + } + } + return root; +}; + +// 迭代法(队列模拟层序遍历) +function invertTree(root: TreeNode | null): TreeNode | null { + const helperQueue: TreeNode[] = []; + let curNode: TreeNode, + tempNode: TreeNode | null; + if (root !== null) helperQueue.push(root); + while (helperQueue.length > 0) { + for (let i = 0, length = helperQueue.length; i < length; i++) { + curNode = helperQueue.shift()!; + tempNode = curNode.left; + curNode.left = curNode.right; + curNode.right = tempNode; + if (curNode.left !== null) helperQueue.push(curNode.left); + if (curNode.right !== null) helperQueue.push(curNode.right); + } + } + return root; +}; +``` + ### C: + 递归法 ```c struct TreeNode* invertTree(struct TreeNode* root){ From cee8536fa671b635f74edb1f7d296a5f2efc1bde Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Sun, 30 Jan 2022 13:17:47 +0800 Subject: [PATCH 15/32] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880101.=E5=AF=B9?= =?UTF-8?q?=E7=A7=B0=E4=BA=8C=E5=8F=89=E6=A0=91.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/0101.对称二叉树.md | 69 ++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/problems/0101.对称二叉树.md b/problems/0101.对称二叉树.md index 69bc41d3..e4e232c8 100644 --- a/problems/0101.对称二叉树.md +++ b/problems/0101.对称二叉树.md @@ -574,6 +574,75 @@ var isSymmetric = function(root) { }; ``` +## TypeScript: + +> 递归法 + +```typescript +function isSymmetric(root: TreeNode | null): boolean { + function recur(node1: TreeNode | null, node2: TreeNode | null): boolean { + if (node1 === null && node2 === null) return true; + if (node1 === null || node2 === null) return false; + if (node1.val !== node2.val) return false + let isSym1: boolean = recur(node1.left, node2.right); + let isSym2: boolean = recur(node1.right, node2.left); + return isSym1 && isSym2 + } + if (root === null) return true; + return recur(root.left, root.right); +}; +``` + +> 迭代法 + +```typescript +// 迭代法(队列) +function isSymmetric(root: TreeNode | null): boolean { + let helperQueue: (TreeNode | null)[] = []; + let tempNode1: TreeNode | null, + tempNode2: TreeNode | null; + if (root !== null) { + helperQueue.push(root.left); + helperQueue.push(root.right); + } + while (helperQueue.length > 0) { + tempNode1 = helperQueue.shift()!; + tempNode2 = helperQueue.shift()!; + if (tempNode1 === null && tempNode2 === null) continue; + if (tempNode1 === null || tempNode2 === null) return false; + if (tempNode1.val !== tempNode2.val) return false; + helperQueue.push(tempNode1.left); + helperQueue.push(tempNode2.right); + helperQueue.push(tempNode1.right); + helperQueue.push(tempNode2.left); + } + return true; +} + +// 迭代法(栈) +function isSymmetric(root: TreeNode | null): boolean { + let helperStack: (TreeNode | null)[] = []; + let tempNode1: TreeNode | null, + tempNode2: TreeNode | null; + if (root !== null) { + helperStack.push(root.left); + helperStack.push(root.right); + } + while (helperStack.length > 0) { + tempNode1 = helperStack.pop()!; + tempNode2 = helperStack.pop()!; + if (tempNode1 === null && tempNode2 === null) continue; + if (tempNode1 === null || tempNode2 === null) return false; + if (tempNode1.val !== tempNode2.val) return false; + helperStack.push(tempNode1.left); + helperStack.push(tempNode2.right); + helperStack.push(tempNode1.right); + helperStack.push(tempNode2.left); + } + return true; +}; +``` + ## Swift: > 递归 From 46571a3b9c619ac39c93becab1c1325fe211b545 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Mon, 31 Jan 2022 14:51:28 +0800 Subject: [PATCH 16/32] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880104.=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E6=9C=80=E5=A4=A7=E6=B7=B1=E5=BA=A6?= =?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/0104.二叉树的最大深度.md | 48 +++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/problems/0104.二叉树的最大深度.md b/problems/0104.二叉树的最大深度.md index 7038598b..6875ec74 100644 --- a/problems/0104.二叉树的最大深度.md +++ b/problems/0104.二叉树的最大深度.md @@ -598,7 +598,55 @@ var maxDepth = function(root) { }; ``` +## TypeScript: + +> 二叉树的最大深度: + +```typescript +// 后续遍历(自下而上) +function maxDepth(root: TreeNode | null): number { + if (root === null) return 0; + return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1; +}; + +// 前序遍历(自上而下) +function maxDepth(root: TreeNode | null): number { + function recur(node: TreeNode | null, count: number) { + if (node === null) { + resMax = resMax > count ? resMax : count; + return; + } + recur(node.left, count + 1); + recur(node.right, count + 1); + } + let resMax: number = 0; + let count: number = 0; + recur(root, count); + return resMax; +}; + +// 层序遍历(迭代法) +function maxDepth(root: TreeNode | null): number { + let helperQueue: TreeNode[] = []; + let resDepth: number = 0; + let tempNode: TreeNode; + if (root !== null) helperQueue.push(root); + while (helperQueue.length > 0) { + resDepth++; + for (let i = 0, length = helperQueue.length; i < length; i++) { + tempNode = helperQueue.shift()!; + if (tempNode.left) helperQueue.push(tempNode.left); + if (tempNode.right) helperQueue.push(tempNode.right); + } + } + return resDepth; +}; +``` + + + ## C + 二叉树最大深度递归 ```c int maxDepth(struct TreeNode* root){ From 463f142f05d5759c8dfaa5f17dbebfa9ddf580f5 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Mon, 31 Jan 2022 16:07:01 +0800 Subject: [PATCH 17/32] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=88559.n=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E6=9C=80=E5=A4=A7=E6=B7=B1=E5=BA=A6=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/0104.二叉树的最大深度.md | 26 +++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/problems/0104.二叉树的最大深度.md b/problems/0104.二叉树的最大深度.md index 6875ec74..3eecdc92 100644 --- a/problems/0104.二叉树的最大深度.md +++ b/problems/0104.二叉树的最大深度.md @@ -643,7 +643,33 @@ function maxDepth(root: TreeNode | null): number { }; ``` +> N叉树的最大深度 +```typescript +// 后续遍历(自下而上) +function maxDepth(root: TreeNode | null): number { + if (root === null) return 0; + return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1; +}; + +// 前序遍历(自上而下) +function maxDepth(root: TreeNode | null): number { + function recur(node: TreeNode | null, count: number) { + if (node === null) { + resMax = resMax > count ? resMax : count; + return; + } + recur(node.left, count + 1); + recur(node.right, count + 1); + } + let resMax: number = 0; + let count: number = 0; + recur(root, count); + return resMax; +}; + + +``` ## C From e4f674c61e71a7b33ec97e22acc6b7a44e075b6e Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Tue, 1 Feb 2022 20:04:51 +0800 Subject: [PATCH 18/32] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880111.=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E6=9C=80=E5=B0=8F=E6=B7=B1=E5=BA=A6?= =?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/0111.二叉树的最小深度.md | 38 +++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/problems/0111.二叉树的最小深度.md b/problems/0111.二叉树的最小深度.md index a439322a..224caa5e 100644 --- a/problems/0111.二叉树的最小深度.md +++ b/problems/0111.二叉树的最小深度.md @@ -404,6 +404,44 @@ var minDepth = function(root) { }; ``` +## TypeScript + +> 递归法 + +```typescript +function minDepth(root: TreeNode | null): number { + if (root === null) return 0; + if (root.left !== null && root.right === null) { + return 1 + minDepth(root.left); + } + if (root.left === null && root.right !== null) { + return 1 + minDepth(root.right); + } + return 1 + Math.min(minDepth(root.left), minDepth(root.right)); +} +``` + +> 迭代法 + +```typescript +function minDepth(root: TreeNode | null): number { + let helperQueue: TreeNode[] = []; + let resMin: number = 0; + let tempNode: TreeNode; + if (root !== null) helperQueue.push(root); + while (helperQueue.length > 0) { + resMin++; + for (let i = 0, length = helperQueue.length; i < length; i++) { + tempNode = helperQueue.shift()!; + if (tempNode.left === null && tempNode.right === null) return resMin; + if (tempNode.left !== null) helperQueue.push(tempNode.left); + if (tempNode.right !== null) helperQueue.push(tempNode.right); + } + } + return resMin; +}; +``` + ## Swift > 递归 From 8ac7cdce22a6f350ec57627e474bd482f5d041b3 Mon Sep 17 00:00:00 2001 From: hutbzc <3260189532@qq.com> Date: Tue, 1 Feb 2022 23:01:36 +0800 Subject: [PATCH 19/32] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880045.=E8=B7=B3?= =?UTF-8?q?=E8=B7=83=E6=B8=B8=E6=88=8FII.md=EF=BC=89=EF=BC=9A=E8=A1=A5?= =?UTF-8?q?=E5=85=85Java=E7=89=88=E6=9C=AC2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0045.跳跃游戏II.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/problems/0045.跳跃游戏II.md b/problems/0045.跳跃游戏II.md index a39c064a..7a3f048c 100644 --- a/problems/0045.跳跃游戏II.md +++ b/problems/0045.跳跃游戏II.md @@ -142,6 +142,7 @@ public: ### Java ```Java +// 版本一 class Solution { public int jump(int[] nums) { if (nums == null || nums.length == 0 || nums.length == 1) { @@ -172,7 +173,30 @@ class Solution { } ``` +```java +// 版本二 +class Solution { + public int jump(int[] nums) { + int result = 0; + // 当前覆盖的最远距离下标 + int end = 0; + // 下一步覆盖的最远距离下标 + int temp = 0; + for (int i = 0; i <= end && end < nums.length - 1; ++i) { + temp = Math.max(temp, i + nums[i]); + // 可达位置的改变次数就是跳跃次数 + if (i == end) { + end = temp; + result++; + } + } + return result; + } +} +``` + ### Python + ```python class Solution: def jump(self, nums: List[int]) -> int: From 40c06155f9e65e3525100ac6aff6f3ce6aeb8dac Mon Sep 17 00:00:00 2001 From: hutbzc <3260189532@qq.com> Date: Tue, 1 Feb 2022 23:04:07 +0800 Subject: [PATCH 20/32] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880139.=E5=8D=95?= =?UTF-8?q?=E8=AF=8D=E6=8B=86=E5=88=86.md=EF=BC=89=EF=BC=9A=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0Java=E5=9B=9E=E5=9B=9E=E6=BA=AF+=E8=AE=B0=E5=BF=86?= =?UTF-8?q?=E5=8C=96=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0139.单词拆分.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/problems/0139.单词拆分.md b/problems/0139.单词拆分.md index e24ffc1a..1653a81a 100644 --- a/problems/0139.单词拆分.md +++ b/problems/0139.单词拆分.md @@ -248,6 +248,36 @@ class Solution { return valid[s.length()]; } } + +// 回溯法+记忆化 +class Solution { + public boolean wordBreak(String s, List wordDict) { + Set wordDictSet = new HashSet(wordDict); + int[] memory = new int[s.length()]; + return backTrack(s, wordDictSet, 0, memory); + } + + public boolean backTrack(String s, Set wordDictSet, int startIndex, int[] memory) { + // 结束条件 + if (startIndex >= s.length()) { + return true; + } + if (memory[startIndex] != 0) { + // 此处认为:memory[i] = 1 表示可以拼出i 及以后的字符子串, memory[i] = -1 表示不能 + return memory[startIndex] == 1 ? true : false; + } + for (int i = startIndex; i < s.length(); ++i) { + // 处理 递归 回溯 循环不变量:[startIndex, i + 1) + String word = s.substring(startIndex, i + 1); + if (wordDictSet.contains(word) && backTrack(s, wordDictSet, i + 1, memory)) { + memory[startIndex] = 1; + return true; + } + } + memory[startIndex] = -1; + return false; + } +} ``` Python: From 5a2ff02b94d417a7a51d44998e212913afeba48b Mon Sep 17 00:00:00 2001 From: hutbzc <3260189532@qq.com> Date: Tue, 1 Feb 2022 23:08:56 +0800 Subject: [PATCH 21/32] =?UTF-8?q?=E4=BF=AE=E6=94=B9=EF=BC=880376.=E6=91=86?= =?UTF-8?q?=E5=8A=A8=E5=BA=8F=E5=88=97.md=EF=BC=89=EF=BC=9A=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E4=BA=86=E9=80=BB=E8=BE=91=E5=B0=8F=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0376.摆动序列.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0376.摆动序列.md b/problems/0376.摆动序列.md index 2bc23182..d75311eb 100644 --- a/problems/0376.摆动序列.md +++ b/problems/0376.摆动序列.md @@ -174,7 +174,7 @@ public: ```Java class Solution { public int wiggleMaxLength(int[] nums) { - if (nums == null || nums.length <= 1) { + if (nums.length <= 1) { return nums.length; } //当前差值 From 2f1c56e6dd34a5a8ae89a8401219a0ce2dd623f3 Mon Sep 17 00:00:00 2001 From: Xiaofei-fei Date: Wed, 2 Feb 2022 18:47:01 +0800 Subject: [PATCH 22/32] =?UTF-8?q?494=E9=A2=98python=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .DS_Store | Bin 0 -> 8196 bytes problems/0494.目标和.md | 3 ++- 2 files changed, 2 insertions(+), 1 deletion(-) create mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..7794435bd6714aed6095604325ab6ffe84221748 GIT binary patch literal 8196 zcmeHMU2GIp6u#fIv@>*|1GG|b$8K6szzU`1r~D<`KM+bOc3ZkFVA<^sbYMDDcV^o{ zC8_Z*g7~0DeNjN;ix1VP7!wtvi3asattOIaj7FnRKFACD;<m0CQ8r?%*`* zup!Jqn1L_@VFtnsgc#mK>qC@oE@Ocl6VhKgsPK;f zB>54*zv!9H0lrT(kg-6H3F*61pW^g@z!kv}1Hzs3QEpB$7RWIng*$_AX9z|{a6*AU zI{8I?bB36ZVHsv1%)s;vaQet>kY$*|QrDl~QTewYB!ubsmP_3Yqqra9{olWg+mlT+n{GUw!6>BxFmbE zVwVEl+2`1MGX~dp^*L5{z;=7J1${}YFKZ<|qu#RO<_Uw^irv19V_AD0*W}%FCp=@s z+c#eT{C4F9d>fSP!^y{nJ6w>vh?lONbu4&8|RHg%m zmHORkhfBfBT2dIUR;$&HV*~|7b45$7x?bVg$!AiU;v1PT@4p;3+(h7x5C#;XGc$n|KQs@D4u2 zC47d@@il(J&-ewu;tyQKU-(*}`EPl8aOHV4$};qPPqUrg~a?_**LUMB<#)$$zerqZ#K$YPAhoU&S> zu&z*-$r>?ej;P+Hu9USFMJo{1s>o{MrL0MQRb3mYRkWn6<%w!lWTPT$5S7j9R?b5$ zUsQE9rYIWoE0M-XGv|^f6Uxsu`LjI>%9Uv=l#lj1Ssi`Gb+E&<$vniKWM>KOAFzw; zOLm$4$bMs22=4`ibPeTLfog0(4BN2-yU>DG>_Zp2(L-oAaM(xuF?h&gm{5NL591L$ zhR5*)p2jnH7SG`$w)*YO6S|2@2qi-i7<@G-u?1it6^4^W#!bE~N5N?f6* zX=ombXKc%{kB~0smvguYWuAEFGJgJVp8fa#n=rEQmBS2#8ThXmKz>uase!C#ThsVi zJ4*K literal 0 HcmV?d00001 diff --git a/problems/0494.目标和.md b/problems/0494.目标和.md index 0faef4a5..f190b734 100644 --- a/problems/0494.目标和.md +++ b/problems/0494.目标和.md @@ -272,7 +272,8 @@ Python: class Solution: def findTargetSumWays(self, nums: List[int], target: int) -> int: sumValue = sum(nums) - if target > sumValue or (sumValue + target) % 2 == 1: return 0 + #注意边界条件为 target>sumValue or target<-sumValue or (sumValue + target) % 2 == 1 + if abs(target) > sumValue or (sumValue + target) % 2 == 1: return 0 bagSize = (sumValue + target) // 2 dp = [0] * (bagSize + 1) dp[0] = 1 From 38c8b6849f32b47d59fc9ed5d1b394dda745b277 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Thu, 3 Feb 2022 20:03:43 +0800 Subject: [PATCH 23/32] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880222.=E5=AE=8C?= =?UTF-8?q?=E5=85=A8=E4=BA=8C=E5=8F=89=E6=A0=91=E7=9A=84=E8=8A=82=E7=82=B9?= =?UTF-8?q?=E4=B8=AA=E6=95=B0.md=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typesc?= =?UTF-8?q?ript=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0222.完全二叉树的节点个数.md | 60 ++++++++++++++++++- 1 file changed, 58 insertions(+), 2 deletions(-) diff --git a/problems/0222.完全二叉树的节点个数.md b/problems/0222.完全二叉树的节点个数.md index 8d38bace..ffbc32ff 100644 --- a/problems/0222.完全二叉树的节点个数.md +++ b/problems/0222.完全二叉树的节点个数.md @@ -447,7 +447,63 @@ var countNodes = function(root) { }; ``` +## TypeScrpt: + +> 递归法 + +```typescript +function countNodes(root: TreeNode | null): number { + if (root === null) return 0; + return 1 + countNodes(root.left) + countNodes(root.right); +}; +``` + +> 迭代法 + +```typescript +function countNodes(root: TreeNode | null): number { + let helperQueue: TreeNode[] = []; + let resCount: number = 0; + let tempNode: TreeNode; + if (root !== null) helperQueue.push(root); + while (helperQueue.length > 0) { + for (let i = 0, length = helperQueue.length; i < length; i++) { + tempNode = helperQueue.shift()!; + resCount++; + if (tempNode.left) helperQueue.push(tempNode.left); + if (tempNode.right) helperQueue.push(tempNode.right); + } + } + return resCount; +}; +``` + +> 利用完全二叉树性质 + +```typescript +function countNodes(root: TreeNode | null): number { + if (root === null) return 0; + let left: number = 0, + right: number = 0; + let curNode: TreeNode | null= root; + while (curNode !== null) { + left++; + curNode = curNode.left; + } + curNode = root; + while (curNode !== null) { + right++; + curNode = curNode.right; + } + if (left === right) { + return 2 ** left - 1; + } + return 1 + countNodes(root.left) + countNodes(root.right); +}; +``` + ## C: + 递归法 ```c int countNodes(struct TreeNode* root) { @@ -538,7 +594,7 @@ func _countNodes(_ root: TreeNode?) -> Int { return 1 + leftCount + rightCount } ``` - + > 层序遍历 ```Swift func countNodes(_ root: TreeNode?) -> Int { @@ -564,7 +620,7 @@ func countNodes(_ root: TreeNode?) -> Int { return res } ``` - + > 利用完全二叉树性质 ```Swift func countNodes(_ root: TreeNode?) -> Int { From ea5a011d783073dc0f98bd5d0a0130c97a02153c Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Sat, 5 Feb 2022 13:04:38 +0800 Subject: [PATCH 24/32] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880110.=E5=B9=B3?= =?UTF-8?q?=E8=A1=A1=E4=BA=8C=E5=8F=89=E6=A0=91.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/0110.平衡二叉树.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/problems/0110.平衡二叉树.md b/problems/0110.平衡二叉树.md index 9d43407a..8a701bb3 100644 --- a/problems/0110.平衡二叉树.md +++ b/problems/0110.平衡二叉树.md @@ -627,7 +627,26 @@ var isBalanced = function(root) { }; ``` +## TypeScript + +```typescript +// 递归法 +function isBalanced(root: TreeNode | null): boolean { + function getDepth(root: TreeNode | null): number { + if (root === null) return 0; + let leftDepth: number = getDepth(root.left); + if (leftDepth === -1) return -1; + let rightDepth: number = getDepth(root.right); + if (rightDepth === -1) return -1; + if (Math.abs(leftDepth - rightDepth) > 1) return -1; + return 1 + Math.max(leftDepth, rightDepth); + } + return getDepth(root) !== -1; +}; +``` + ## C + 递归法: ```c int getDepth(struct TreeNode* node) { From f9e5bce7d7b8b53d266ba082036677b30d092696 Mon Sep 17 00:00:00 2001 From: jobinben <1021137079@qq.com> Date: Sun, 6 Feb 2022 18:36:14 +0800 Subject: [PATCH 25/32] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=E9=9D=A2=E8=AF=95?= =?UTF-8?q?=E9=A2=9802.07.=E9=93=BE=E8=A1=A8=E7=9B=B8=E4=BA=A4.md=20JavaSc?= =?UTF-8?q?ript=E8=AF=AD=E8=A8=80=E8=A7=A3=E6=B3=95=E7=9A=84=E6=B3=A8?= =?UTF-8?q?=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/面试题02.07.链表相交.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/problems/面试题02.07.链表相交.md b/problems/面试题02.07.链表相交.md index dd91f069..2e7226de 100644 --- a/problems/面试题02.07.链表相交.md +++ b/problems/面试题02.07.链表相交.md @@ -224,12 +224,14 @@ var getIntersectionNode = function(headA, headB) { lenA = getListLen(headA), lenB = getListLen(headB); if(lenA < lenB) { + // 下面交换变量注意加 “分号” ,两个数组交换变量在同一个作用域下时 + // 如果不加分号,下面两条代码等同于一条代码: [curA, curB] = [lenB, lenA] [curA, curB] = [curB, curA]; [lenA, lenB] = [lenB, lenA]; } let i = lenA - lenB; while(i-- > 0) { - curA = curA.next + curA = curA.next; } while(curA && curA !== curB) { curA = curA.next; From 343ddb37f9e84284ff8df34f89bed18522244e43 Mon Sep 17 00:00:00 2001 From: bqlin Date: Mon, 20 Dec 2021 20:13:42 +0800 Subject: [PATCH 26/32] =?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 27/32] =?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 28/32] =?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 29/32] =?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 be6c45c636556b1c0d3838f265fe7830ca4f00ff Mon Sep 17 00:00:00 2001 From: jobinben <1021137079@qq.com> Date: Sun, 6 Feb 2022 19:06:51 +0800 Subject: [PATCH 30/32] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200110.=E5=B9=B3?= =?UTF-8?q?=E8=A1=A1=E4=BA=8C=E5=8F=89=E6=A0=91.md=20JavaScript=E8=AF=AD?= =?UTF-8?q?=E8=A8=80=E7=89=88=E6=9C=AC=E7=9A=84=E8=BF=AD=E4=BB=A3=E8=A7=A3?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0110.平衡二叉树.md | 45 +++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/problems/0110.平衡二叉树.md b/problems/0110.平衡二叉树.md index 9d43407a..8a11c013 100644 --- a/problems/0110.平衡二叉树.md +++ b/problems/0110.平衡二叉树.md @@ -604,7 +604,8 @@ func abs(a int)int{ } ``` -## JavaScript +## JavaScript +递归法: ```javascript var isBalanced = function(root) { //还是用递归三部曲 + 后序遍历 左右中 当前左子树右子树高度相差大于1就返回-1 @@ -627,6 +628,48 @@ var isBalanced = function(root) { }; ``` +迭代法: +```javascript +// 获取当前节点的高度 +var getHeight = function (curNode) { + let queue = []; + if (curNode !== null) queue.push(curNode); // 压入当前元素 + let depth = 0, res = 0; + while (queue.length) { + let node = queue[queue.length - 1]; // 取出栈顶 + if (node !== null) { + queue.pop(); + queue.push(node); // 中 + queue.push(null); + depth++; + node.right && queue.push(node.right); // 右 + node.left && queue.push(node.left); // 左 + } else { + queue.pop(); + node = queue[queue.length - 1]; + queue.pop(); + depth--; + } + res = res > depth ? res : depth; + } + return res; +} +var isBalanced = function (root) { + if (root === null) return true; + let queue = [root]; + while (queue.length) { + let node = queue[queue.length - 1]; // 取出栈顶 + queue.pop(); + if (Math.abs(getHeight(node.left) - getHeight(node.right)) > 1) { + return false; + } + node.right && queue.push(node.right); + node.left && queue.push(node.left); + } + return true; +}; +``` + ## C 递归法: ```c 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 31/32] =?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 266702c291505ab3847d520ab7474789d0e97fab Mon Sep 17 00:00:00 2001 From: youngyangyang04 <826123027@qq.com> Date: Fri, 18 Feb 2022 22:09:38 +0800 Subject: [PATCH 32/32] Update --- problems/前序/ACM模式如何构建二叉树.md | 3 --- ...AT级别技术面试流程和注意事项都在这里了.md | 3 --- ...n的算法居然超时了,此时的n究竟是多大?.md | 3 --- problems/前序/上海互联网公司总结.md | 3 --- .../什么是核心代码模式,什么又是ACM模式?.md | 3 --- .../关于时间复杂度,你不知道的都在这里!.md | 3 --- .../前序/关于空间复杂度,可能有几个疑问?.md | 3 --- ...么多题,你了解自己代码的内存消耗么?.md | 3 --- .../前序/力扣上的代码想在本地编译运行?.md | 3 --- problems/前序/北京互联网公司总结.md | 3 --- problems/前序/广州互联网公司总结.md | 3 --- problems/前序/成都互联网公司总结.md | 3 --- problems/前序/杭州互联网公司总结.md | 3 --- problems/前序/深圳互联网公司总结.md | 3 --- problems/前序/程序员写文档工具.md | 3 --- problems/前序/程序员简历.md | 3 --- .../前序/递归算法的时间与空间复杂度分析.md | 3 --- ...试题目,讲一讲递归算法的时间复杂度!.md | 3 --- 18 files changed, 54 deletions(-) diff --git a/problems/前序/ACM模式如何构建二叉树.md b/problems/前序/ACM模式如何构建二叉树.md index d3b2656e..fc7a1823 100644 --- a/problems/前序/ACM模式如何构建二叉树.md +++ b/problems/前序/ACM模式如何构建二叉树.md @@ -251,7 +251,4 @@ int main() { ``` ----------------------- -* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) -* B站视频:[代码随想录](https://space.bilibili.com/525438321) -* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
diff --git a/problems/前序/BAT级别技术面试流程和注意事项都在这里了.md b/problems/前序/BAT级别技术面试流程和注意事项都在这里了.md index c5797739..6678860d 100644 --- a/problems/前序/BAT级别技术面试流程和注意事项都在这里了.md +++ b/problems/前序/BAT级别技术面试流程和注意事项都在这里了.md @@ -218,7 +218,4 @@ leetcode是专门针对算法练习的题库,leetcode现在也推出了中文 ----------------------- -* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) -* B站视频:[代码随想录](https://space.bilibili.com/525438321) -* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
diff --git a/problems/前序/On的算法居然超时了,此时的n究竟是多大?.md b/problems/前序/On的算法居然超时了,此时的n究竟是多大?.md index 9a56937c..5257ceb9 100644 --- a/problems/前序/On的算法居然超时了,此时的n究竟是多大?.md +++ b/problems/前序/On的算法居然超时了,此时的n究竟是多大?.md @@ -280,7 +280,4 @@ public class TimeComplexity { ----------------------- -* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) -* B站视频:[代码随想录](https://space.bilibili.com/525438321) -* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
diff --git a/problems/前序/上海互联网公司总结.md b/problems/前序/上海互联网公司总结.md index 08c15895..ffcbe77b 100644 --- a/problems/前序/上海互联网公司总结.md +++ b/problems/前序/上海互联网公司总结.md @@ -130,7 +130,4 @@ ----------------------- -* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) -* B站视频:[代码随想录](https://space.bilibili.com/525438321) -* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
diff --git a/problems/前序/什么是核心代码模式,什么又是ACM模式?.md b/problems/前序/什么是核心代码模式,什么又是ACM模式?.md index 3c5fb4e4..54c5b6ec 100644 --- a/problems/前序/什么是核心代码模式,什么又是ACM模式?.md +++ b/problems/前序/什么是核心代码模式,什么又是ACM模式?.md @@ -119,7 +119,4 @@ int main() { ----------------------- -* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) -* B站视频:[代码随想录](https://space.bilibili.com/525438321) -* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
diff --git a/problems/前序/关于时间复杂度,你不知道的都在这里!.md b/problems/前序/关于时间复杂度,你不知道的都在这里!.md index cfcbed1a..478b82e4 100644 --- a/problems/前序/关于时间复杂度,你不知道的都在这里!.md +++ b/problems/前序/关于时间复杂度,你不知道的都在这里!.md @@ -170,7 +170,4 @@ $O(2 × n^2 + 10 × n + 1000) < O(3 × n^2)$,所以说最后省略掉常数项 ----------------------- -* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) -* B站视频:[代码随想录](https://space.bilibili.com/525438321) -* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
diff --git a/problems/前序/关于空间复杂度,可能有几个疑问?.md b/problems/前序/关于空间复杂度,可能有几个疑问?.md index 95ffe597..d49b42a2 100644 --- a/problems/前序/关于空间复杂度,可能有几个疑问?.md +++ b/problems/前序/关于空间复杂度,可能有几个疑问?.md @@ -73,7 +73,4 @@ for (int i = 0; i < n; i++) { ----------------------- -* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) -* B站视频:[代码随想录](https://space.bilibili.com/525438321) -* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
diff --git a/problems/前序/刷了这么多题,你了解自己代码的内存消耗么?.md b/problems/前序/刷了这么多题,你了解自己代码的内存消耗么?.md index 3fccfb22..0364fc8b 100644 --- a/problems/前序/刷了这么多题,你了解自己代码的内存消耗么?.md +++ b/problems/前序/刷了这么多题,你了解自己代码的内存消耗么?.md @@ -150,7 +150,4 @@ char型的数据和int型的数据挨在一起,该int数据从地址1开始, ----------------------- -* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) -* B站视频:[代码随想录](https://space.bilibili.com/525438321) -* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
diff --git a/problems/前序/力扣上的代码想在本地编译运行?.md b/problems/前序/力扣上的代码想在本地编译运行?.md index c4899a20..dca6eec3 100644 --- a/problems/前序/力扣上的代码想在本地编译运行?.md +++ b/problems/前序/力扣上的代码想在本地编译运行?.md @@ -67,7 +67,4 @@ int main() { ----------------------- -* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) -* B站视频:[代码随想录](https://space.bilibili.com/525438321) -* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
diff --git a/problems/前序/北京互联网公司总结.md b/problems/前序/北京互联网公司总结.md index 0e22dad6..02a877b7 100644 --- a/problems/前序/北京互联网公司总结.md +++ b/problems/前序/北京互联网公司总结.md @@ -116,7 +116,4 @@ ----------------------- -* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) -* B站视频:[代码随想录](https://space.bilibili.com/525438321) -* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
diff --git a/problems/前序/广州互联网公司总结.md b/problems/前序/广州互联网公司总结.md index ae41c899..1cf0da36 100644 --- a/problems/前序/广州互联网公司总结.md +++ b/problems/前序/广州互联网公司总结.md @@ -79,7 +79,4 @@ ----------------------- -* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) -* B站视频:[代码随想录](https://space.bilibili.com/525438321) -* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
diff --git a/problems/前序/成都互联网公司总结.md b/problems/前序/成都互联网公司总结.md index d44800cd..7964f23c 100644 --- a/problems/前序/成都互联网公司总结.md +++ b/problems/前序/成都互联网公司总结.md @@ -77,7 +77,4 @@ ----------------------- -* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) -* B站视频:[代码随想录](https://space.bilibili.com/525438321) -* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
diff --git a/problems/前序/杭州互联网公司总结.md b/problems/前序/杭州互联网公司总结.md index 326a176b..029ee380 100644 --- a/problems/前序/杭州互联网公司总结.md +++ b/problems/前序/杭州互联网公司总结.md @@ -87,7 +87,4 @@ ----------------------- -* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) -* B站视频:[代码随想录](https://space.bilibili.com/525438321) -* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
diff --git a/problems/前序/深圳互联网公司总结.md b/problems/前序/深圳互联网公司总结.md index 9e089315..61bd52e8 100644 --- a/problems/前序/深圳互联网公司总结.md +++ b/problems/前序/深圳互联网公司总结.md @@ -82,7 +82,4 @@ ----------------------- -* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) -* B站视频:[代码随想录](https://space.bilibili.com/525438321) -* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
diff --git a/problems/前序/程序员写文档工具.md b/problems/前序/程序员写文档工具.md index e4193c42..5504ae7a 100644 --- a/problems/前序/程序员写文档工具.md +++ b/problems/前序/程序员写文档工具.md @@ -136,7 +136,4 @@ Markdown支持部分html,例如这样 ----------------------- -* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) -* B站视频:[代码随想录](https://space.bilibili.com/525438321) -* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
diff --git a/problems/前序/程序员简历.md b/problems/前序/程序员简历.md index f47516dc..522fc5f7 100644 --- a/problems/前序/程序员简历.md +++ b/problems/前序/程序员简历.md @@ -133,7 +133,4 @@ Carl校招社招都拿过大厂的offer,同时也看过很多应聘者的简 ----------------------- -* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) -* B站视频:[代码随想录](https://space.bilibili.com/525438321) -* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
diff --git a/problems/前序/递归算法的时间与空间复杂度分析.md b/problems/前序/递归算法的时间与空间复杂度分析.md index 4dd340a6..914cccfd 100644 --- a/problems/前序/递归算法的时间与空间复杂度分析.md +++ b/problems/前序/递归算法的时间与空间复杂度分析.md @@ -269,7 +269,4 @@ int binary_search( int arr[], int l, int r, int x) { ----------------------- -* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) -* B站视频:[代码随想录](https://space.bilibili.com/525438321) -* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
diff --git a/problems/前序/通过一道面试题目,讲一讲递归算法的时间复杂度!.md b/problems/前序/通过一道面试题目,讲一讲递归算法的时间复杂度!.md index 8780122f..849a025d 100644 --- a/problems/前序/通过一道面试题目,讲一讲递归算法的时间复杂度!.md +++ b/problems/前序/通过一道面试题目,讲一讲递归算法的时间复杂度!.md @@ -152,7 +152,4 @@ int function3(int x, int n) { ----------------------- -* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) -* B站视频:[代码随想录](https://space.bilibili.com/525438321) -* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)