From c7c211c30bcfd5f806349d8a030e3c49eb204eab Mon Sep 17 00:00:00 2001 From: SwaggyP <1352164869@qq.com> Date: Fri, 19 Aug 2022 16:52:06 +0800 Subject: [PATCH 1/3] =?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 2/3] =?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 41c480371c4a9c41f9e0a06d35810529b15c8c91 Mon Sep 17 00:00:00 2001 From: SwaggyP <1352164869@qq.com> Date: Wed, 24 Aug 2022 12:41:38 +0800 Subject: [PATCH 3/3] =?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] +} + ```