From 642734522dcb0aa693ab651a6591fd315a721db2 Mon Sep 17 00:00:00 2001 From: ekertree Date: Thu, 18 Aug 2022 20:07:34 +0800 Subject: [PATCH 01/10] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200844=E6=AF=94?= =?UTF-8?q?=E8=BE=83=E5=90=AB=E9=80=80=E6=A0=BC=E7=9A=84=E5=AD=97=E7=AC=A6?= =?UTF-8?q?=E4=B8=B2=20Java=20=E7=89=88=E6=9C=AC=20=E5=88=A0=E9=99=A4=20Ja?= =?UTF-8?q?va=E6=A0=87=E9=A2=98=E7=9A=84=E5=86=92=E5=8F=B7=20=E7=BB=9F?= =?UTF-8?q?=E4=B8=80=E9=A1=B5=E9=9D=A2=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0844.比较含退格的字符串.md | 75 +++++++++++++++++++- 1 file changed, 74 insertions(+), 1 deletion(-) diff --git a/problems/0844.比较含退格的字符串.md b/problems/0844.比较含退格的字符串.md index f7823e59..5d29ffb0 100644 --- a/problems/0844.比较含退格的字符串.md +++ b/problems/0844.比较含退格的字符串.md @@ -157,7 +157,7 @@ public: ## 其他语言版本 -### Java: +### Java ```java // 普通方法(使用栈的思路) @@ -214,7 +214,80 @@ public static boolean backspaceCompare(String s, String t) { } ``` +```java +class Solution { + public static boolean backspaceCompare(String s, String t) { + return getStr(s).equals(getStr(t)); + } + public static String getStr(String s) { //使用快慢双指针去除字符串中的# + int slowIndex; + int fastIndex = 0; + StringBuilder builder = new StringBuilder(s); //StringBuilder用于修改字符串 + for(slowIndex = 0; fastIndex < s.length(); fastIndex++) { + if(builder.charAt(fastIndex) != '#') { + builder.setCharAt(slowIndex++,builder.charAt(fastIndex)); + } else { + if(slowIndex > 0) { + slowIndex--; + } + } + } + return builder.toString().substring(0,slowIndex); //截取有效字符串 + } +} +``` + +从后往前双指针: + +```java +class Solution { + public static boolean backspaceCompare(String s, String t) { + int sSkipNum = 0; //记录s的#的个数 + int tSkipNum = 0; //记录t的#的个数 + int sIndex = s.length() - 1; + int tIndex = t.length() - 1; + while(true) { + while(sIndex >= 0) { //每次记录连续的#并跳过被删除的字符 + if(s.charAt(sIndex) == '#') { + sSkipNum++; + } else { + if(sSkipNum > 0) { + sSkipNum--; + } else { + break; + } + } + sIndex--; + } + while(tIndex >= 0) { //每次记录连续的#并跳过被删除的字符 + if(t.charAt(tIndex) == '#') { + tSkipNum++; + } else { + if(tSkipNum > 0) { + tSkipNum--; + } else { + break; + } + } + tIndex--; + } + if(sIndex < 0 || tIndex < 0) { //s 或者 t遍历完了 + break; + } + if(s.charAt(sIndex) != t.charAt(tIndex)) { //当前下标的字符不相等 + return false; + } + sIndex--; + tIndex--; + } + if(sIndex == -1 && tIndex == -1) { //同时遍历完 则相等 + return true; + } + return false; + } +} +``` ### python From 058f64624a062083aeb78ea5589c6e22ce444a12 Mon Sep 17 00:00:00 2001 From: wang2jun <91008685+wang2jun@users.noreply.github.com> Date: Fri, 19 Aug 2022 10:42:17 +0800 Subject: [PATCH 02/10] =?UTF-8?q?0100=E7=9B=B8=E5=90=8C=E7=9A=84=E6=A0=91?= =?UTF-8?q?=20=E5=A2=9E=E5=8A=A0=20js=20=E8=BF=AD=E4=BB=A3=E6=B3=95?= =?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/0100.相同的树.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/problems/0100.相同的树.md b/problems/0100.相同的树.md index 4b6eb7aa..9820173d 100644 --- a/problems/0100.相同的树.md +++ b/problems/0100.相同的树.md @@ -253,6 +253,28 @@ var isSameTree = function (p, q) { return isSameTree(p.left, q.left) && isSameTree(p.right, q.right); }; ``` +> 迭代法 + +```javascript +var isSameTree = (p, q) => { + const queue = [{ p, q }]; + // 这是用{ } 解决了null的问题! + while (queue.length) { + const cur = queue.shift(); + if (cur.p == null && cur.q == null) continue; + if (cur.p == null || cur.q == null) return false; + if (cur.p.val != cur.q.val) return false; + queue.push({ + p: cur.p.left, + q: cur.q.left + }, { + p: cur.p.right, + q: cur.q.right + }); + } + return true; +}; +``` TypeScript: From 10734b6f20a881532b74f6313023717830e756dd Mon Sep 17 00:00:00 2001 From: marspere <1587393449@qq.com> Date: Fri, 19 Aug 2022 10:50:33 +0800 Subject: [PATCH 03/10] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E5=B1=82=E5=BA=8F=E9=81=8D=E5=8E=86116=E5=92=8C117?= =?UTF-8?q?=E9=A2=98=E7=9A=84go=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0102.二叉树的层序遍历.md | 69 +++++++++++------------ 1 file changed, 33 insertions(+), 36 deletions(-) diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md index 9ad34494..69f2f594 100644 --- a/problems/0102.二叉树的层序遍历.md +++ b/problems/0102.二叉树的层序遍历.md @@ -1891,36 +1891,35 @@ go: */ func connect(root *Node) *Node { - res:=[][]*Node{} - if root==nil{//防止为空 + if root == nil { //防止为空 return root } - queue:=list.New() + queue := list.New() queue.PushBack(root) - var tmpArr []*Node - for queue.Len()>0 { - length:=queue.Len()//保存当前层的长度,然后处理当前层(十分重要,防止添加下层元素影响判断层中元素的个数) - for i:=0;i 0 { + length := queue.Len() //保存当前层的长度,然后处理当前层(十分重要,防止添加下层元素影响判断层中元素的个数) + for i := 0; i < length; i++ { + node := queue.Remove(queue.Front()).(*Node) //出队列 + if node.Left != nil { queue.PushBack(node.Left) } - if node.Right!=nil{ + if node.Right != nil { queue.PushBack(node.Right) } - tmpArr=append(tmpArr,node)//将值加入本层切片中 + tmpArr = append(tmpArr, node) //将值加入本层切片中 } - res=append(res,tmpArr)//放入结果集 - tmpArr=[]*Node{}//清空层的数据 - } - //遍历每层元素,指定next - for i:=0;i 1 { + // 遍历每层元素,指定next + for i := 0; i < len(tmpArr)-1; i++ { + tmpArr[i].Next = tmpArr[i+1] + } } + tmpArr = []*Node{} //清空层的数据 } return root } + ``` Swift: @@ -2172,33 +2171,31 @@ go: */ func connect(root *Node) *Node { - res:=[][]*Node{} - if root==nil{//防止为空 + if root == nil { //防止为空 return root } - queue:=list.New() + queue := list.New() queue.PushBack(root) - var tmpArr []*Node - for queue.Len()>0 { - length:=queue.Len()//保存当前层的长度,然后处理当前层(十分重要,防止添加下层元素影响判断层中元素的个数) - for i:=0;i 0 { + length := queue.Len() //保存当前层的长度,然后处理当前层(十分重要,防止添加下层元素影响判断层中元素的个数) + for i := 0; i < length; i++ { + node := queue.Remove(queue.Front()).(*Node) //出队列 + if node.Left != nil { queue.PushBack(node.Left) } - if node.Right!=nil{ + if node.Right != nil { queue.PushBack(node.Right) } - tmpArr=append(tmpArr,node)//将值加入本层切片中 + tmpArr = append(tmpArr, node) //将值加入本层切片中 } - res=append(res,tmpArr)//放入结果集 - tmpArr=[]*Node{}//清空层的数据 - } - //遍历每层元素,指定next - for i:=0;i 1 { + // 遍历每层元素,指定next + for i := 0; i < len(tmpArr)-1; i++ { + tmpArr[i].Next = tmpArr[i+1] + } } + tmpArr = []*Node{} //清空层的数据 } return root } From 4f71ddc10fdacabdb14acfd53cbcee7bb0d7e996 Mon Sep 17 00:00:00 2001 From: marspere <1587393449@qq.com> Date: Fri, 19 Aug 2022 11:28:57 +0800 Subject: [PATCH 04/10] =?UTF-8?q?Update=200102:=E4=BC=98=E5=8C=96=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E5=B1=82=E5=BA=8F=E9=81=8D=E5=8E=86=E4=B8=AD?= =?UTF-8?q?515=E9=A2=98=E7=9A=84go=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0102.二叉树的层序遍历.md | 52 +++++++++++------------ 1 file changed, 24 insertions(+), 28 deletions(-) diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md index 69f2f594..a1a7baed 100644 --- a/problems/0102.二叉树的层序遍历.md +++ b/problems/0102.二叉树的层序遍历.md @@ -1552,44 +1552,40 @@ go: 515. 在每个树行中找最大值 */ func largestValues(root *TreeNode) []int { - res:=[][]int{} - var finRes []int - if root==nil{//防止为空 - return finRes + if root == nil { + //防止为空 + return nil } - queue:=list.New() + queue := list.New() queue.PushBack(root) - var tmpArr []int - //层次遍历 - for queue.Len()>0 { - length:=queue.Len()//保存当前层的长度,然后处理当前层(十分重要,防止添加下层元素影响判断层中元素的个数) - for i:=0;i 0 { + //保存当前层的长度,然后处理当前层(十分重要,防止添加下层元素影响判断层中元素的个数) + length := queue.Len() + for i := 0; i < length; i++ { + node := queue.Remove(queue.Front()).(*TreeNode)//出队列 + // 比较当前层中的最大值和新遍历的元素大小,取两者中大值 + temp = max(temp, node.Val) + if node.Left != nil { queue.PushBack(node.Left) } - if node.Right!=nil{ + if node.Right != nil { queue.PushBack(node.Right) } - tmpArr=append(tmpArr,node.Val)//将值加入本层切片中 } - res=append(res,tmpArr)//放入结果集 - tmpArr=[]int{}//清空层的数据 + ans = append(ans, temp) + temp = math.MinInt64 } - //找到每层的最大值 - for i:=0;i max { - max = val - } + +func max(x, y int) int { + if x > y { + return x } - return max + return y } ``` From 33c079738631c552185ba8476c893b90ecf477d0 Mon Sep 17 00:00:00 2001 From: marspere <1587393449@qq.com> Date: Fri, 19 Aug 2022 13:50:48 +0800 Subject: [PATCH 05/10] =?UTF-8?q?Update=200102:=E4=BC=98=E5=8C=96=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?=E4=B8=AD637=E9=A2=98=E7=9A=84go=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0102.二叉树的层序遍历.md | 46 ++++++++++------------- 1 file changed, 20 insertions(+), 26 deletions(-) diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md index a1a7baed..25189dba 100644 --- a/problems/0102.二叉树的层序遍历.md +++ b/problems/0102.二叉树的层序遍历.md @@ -1054,40 +1054,34 @@ go: 637. 二叉树的层平均值 */ func averageOfLevels(root *TreeNode) []float64 { - res:=[][]int{} - var finRes []float64 - if root==nil{//防止为空 - return finRes + if root == nil { + // 防止为空 + return nil } - queue:=list.New() + res := make([]float64, 0) + queue := list.New() queue.PushBack(root) - var tmpArr []int - for queue.Len()>0 { - length:=queue.Len()//保存当前层的长度,然后处理当前层(十分重要,防止添加下层元素影响判断层中元素的个数) - for i:=0;i 0 { + //保存当前层的长度,然后处理当前层(十分重要,防止添加下层元素影响判断层中元素的个数) + length := queue.Len() + for i := 0; i < length; i++ { + node := queue.Remove(queue.Front()).(*TreeNode) + if node.Left != nil { queue.PushBack(node.Left) } - if node.Right!=nil{ + if node.Right != nil { queue.PushBack(node.Right) } - tmpArr=append(tmpArr,node.Val)//将值加入本层切片中 + // 当前层元素求和 + sum += node.Val } - res=append(res,tmpArr)//放入结果集 - tmpArr=[]int{}//清空层的数据 + // 计算每层的平均值,将结果添加到响应结果中 + res = append(res, float64(sum)/float64(length)) + sum = 0 // 清空该层的数据 } - //计算每层的平均值 - length:=len(res) - for i:=0;i Date: Fri, 19 Aug 2022 13:56:47 +0800 Subject: [PATCH 06/10] =?UTF-8?q?Update=200102:=E4=BC=98=E5=8C=96=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?=E4=B8=AD199=E9=A2=98=E7=9A=84go=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0102.二叉树的层序遍历.md | 35 +++++++++++------------ 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md index 25189dba..0063f36e 100644 --- a/problems/0102.二叉树的层序遍历.md +++ b/problems/0102.二叉树的层序遍历.md @@ -810,33 +810,30 @@ go: 199. 二叉树的右视图 */ func rightSideView(root *TreeNode) []int { - queue:=list.New() - res:=[][]int{} - var finaRes []int - if root==nil{ - return finaRes + if root == nil { + return nil } + res := make([]int, 0) + queue := list.New() queue.PushBack(root) - for queue.Len()>0{ - length:=queue.Len() - tmp:=[]int{} - for i:=0;i 0 { + length := queue.Len() + for i := 0; i < length; i++ { + node := queue.Remove(queue.Front()).(*TreeNode) + if node.Left != nil { queue.PushBack(node.Left) } - if node.Right!=nil{ + if node.Right != nil { queue.PushBack(node.Right) } - tmp=append(tmp,node.Val) + // 取每层的最后一个元素,添加到结果集中 + if i == length-1 { + res = append(res, node.Val) + } } - res=append(res,tmp) } - //取每一层的最后一个元素 - for i:=0;i Date: Fri, 19 Aug 2022 16:52:06 +0800 Subject: [PATCH 07/10] =?UTF-8?q?=E6=9B=B4=E6=96=B00129.=E6=B1=82=E6=A0=B9?= =?UTF-8?q?=E5=88=B0=E5=8F=B6=E5=AD=90=E7=BB=93=E7=82=B9=E6=95=B0=E5=AD=97?= =?UTF-8?q?=E4=B9=8B=E5=92=8C=E4=B8=AD=EF=BC=8Cgo=E8=AF=AD=E8=A8=80?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E6=97=A0=E6=B3=95=E9=80=9A=E8=BF=87leetcode?= =?UTF-8?q?=E7=9A=84=E8=AF=AD=E6=B3=95=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改了原本的go代码的错误(原本的go代码无法通过leetcode的题目执行,有语法错误) --- .../0129.求根到叶子节点数字之和.md | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/problems/0129.求根到叶子节点数字之和.md b/problems/0129.求根到叶子节点数字之和.md index 92a72fe3..859df39e 100644 --- a/problems/0129.求根到叶子节点数字之和.md +++ b/problems/0129.求根到叶子节点数字之和.md @@ -250,22 +250,22 @@ Go: ```go func sumNumbers(root *TreeNode) int { - sum = 0 - travel(root, root.Val) - return sum + sum := 0 + dfs(root, root.Val, &sum) + return sum } -func travel(root *TreeNode, tmpSum int) { - if root.Left == nil && root.Right == nil { - sum += tmpSum - } else { - if root.Left != nil { - travel(root.Left, tmpSum*10+root.Left.Val) - } - if root.Right != nil { - travel(root.Right, tmpSum*10+root.Right.Val) - } - } +func dfs(root *TreeNode, tmpSum int, sum *int) { + if root.Left == nil && root.Right == nil { + *sum += tmpSum + } else { + if root.Left != nil { + dfs(root.Left, tmpSum*10 + root.Left.Val, sum) + } + if root.Right != nil { + dfs(root.Right, tmpSum*10 + root.Right.Val, sum) + } + } } ``` From 87a51bcb6916834fa98dc6f2d3dd1f0df69a6ac9 Mon Sep 17 00:00:00 2001 From: SwaggyP <1352164869@qq.com> Date: Sat, 20 Aug 2022 09:53:47 +0800 Subject: [PATCH 08/10] =?UTF-8?q?=E5=A2=9E=E5=8A=A00100.=E7=9B=B8=E5=90=8C?= =?UTF-8?q?=E7=9A=84=E6=A0=91go=E8=AF=AD=E8=A8=80=E7=9A=84=E9=80=92?= =?UTF-8?q?=E5=BD=92=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加了0100.相同的树go语言的递归解法 --- problems/0100.相同的树.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/problems/0100.相同的树.md b/problems/0100.相同的树.md index 4b6eb7aa..0057a85e 100644 --- a/problems/0100.相同的树.md +++ b/problems/0100.相同的树.md @@ -237,6 +237,26 @@ class Solution: return True ``` Go: +> 递归法 +```go +func isSameTree(p *TreeNode, q *TreeNode) bool { + if p != nil && q == nil { + return false + } + if p == nil && q != nil { + return false + } + if p == nil && q == nil { + return true + } + if p.Val != q.Val { + return false + } + Left := isSameTree(p.Left, q.Left) + Right := isSameTree(p.Right, q.Right) + return Left && Right +} +``` JavaScript: From 8e3524d482764d29e3237cc85bc6daceb4c82b53 Mon Sep 17 00:00:00 2001 From: ekertree Date: Sat, 20 Aug 2022 21:02:18 +0800 Subject: [PATCH 09/10] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=2054.=E8=9E=BA?= =?UTF-8?q?=E6=97=8B=E7=9F=A9=E9=98=B5=20Java=20=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0054.螺旋矩阵.md | 68 +++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/problems/0054.螺旋矩阵.md b/problems/0054.螺旋矩阵.md index efbda5ff..aeb8e521 100644 --- a/problems/0054.螺旋矩阵.md +++ b/problems/0054.螺旋矩阵.md @@ -133,5 +133,73 @@ public: ## 其他语言版本 +### Java + +```java +class Solution { + public List spiralOrder(int[][] matrix) { + //存放数组的数 + List ans = new ArrayList<>(); + //列数 + int columns = matrix[0].length; + //行数 + int rows = matrix.length; + //遍历起点 + int start = 0; + //循环的次数 行数和列数中的最小值除以二 + int loop = Math.min(rows,columns) / 2; + //未遍历的中间列(行)的列(行)下标 + int mid = loop; + //终止条件 + int offSet = 1; + int i,j; + while(loop-- > 0) { + //初始化起点 + i = j = start; + + //从左往右 + for(; j < columns - offSet; j++) + ans.add(matrix[i][j]); + + //从上往下 + for(; i < rows - offSet; i++) + ans.add(matrix[i][j]); + + //从右往左 + for(; j > start; j--) + ans.add(matrix[i][j]); + + //从下往上 + for(; i > start; i--) + ans.add(matrix[i][j]); + + //每循环一次 改变起点位置 + start++; + //终止条件改变 + offSet++; + } + + //如果行和列中的最小值是奇数 则会产生中间行或者中间列没有遍历 + if(Math.min(rows,columns) % 2 != 0) { + //行大于列则产生中间列 + if(rows > columns) { + //中间列的行的最大下标的下一位的下标为mid + rows - columns + 1 + for(int k = mid; k < mid + rows - columns + 1; k++) { + ans.add(matrix[k][mid]); + } + }else {//列大于等于行则产生中间行 + //中间行的列的最大下标的下一位的下标为mid + columns - rows + 1 + for(int k = mid; k < mid + columns - rows + 1; k++) { + ans.add(matrix[mid][k]); + } + } + } + return ans; + } +} +``` + + + -----------------------
From 41c480371c4a9c41f9e0a06d35810529b15c8c91 Mon Sep 17 00:00:00 2001 From: SwaggyP <1352164869@qq.com> Date: Wed, 24 Aug 2022 12:41:38 +0800 Subject: [PATCH 10/10] =?UTF-8?q?0005.=E6=9C=80=E9=95=BF=E5=9B=9E=E6=96=87?= =?UTF-8?q?=E5=AD=90=E4=B8=B2=E5=A2=9E=E5=8A=A0go=E7=9A=84dp=E8=A7=A3?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 0005.最长回文子串增加go的dp解法 --- problems/0005.最长回文子串.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/problems/0005.最长回文子串.md b/problems/0005.最长回文子串.md index d53acf63..6e407d6e 100644 --- a/problems/0005.最长回文子串.md +++ b/problems/0005.最长回文子串.md @@ -363,6 +363,34 @@ class Solution: Go: ```go +func longestPalindrome(s string) string { + maxLen := 0 + left := 0 + length := 0 + dp := make([][]bool, len(s)) + for i := 0; i < len(s); i++ { + dp[i] = make([]bool,len(s)) + } + for i := len(s)-1; i >= 0; i-- { + for j := i; j < len(s); j++ { + if s[i] == s[j]{ + if j-i <= 1{ // 情况一和情况二 + length = j-i + dp[i][j]=true + }else if dp[i+1][j-1]{ // 情况三 + length = j-i + dp[i][j] = true + } + } + } + if length > maxLen { + maxLen = length + left = i + } + } + return s[left: left+maxLen+1] +} + ```