From 8e44f0a11b1f31c03b8cbcc1c3cda4d37042d1ff Mon Sep 17 00:00:00 2001 From: X-shuffle <53906918+X-shuffle@users.noreply.github.com> Date: Sat, 12 Jun 2021 19:56:09 +0800 Subject: [PATCH 01/13] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20404.=E5=B7=A6?= =?UTF-8?q?=E5=8F=B6=E5=AD=90=E4=B9=8B=E5=92=8C=20Javascript=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 404.左叶子之和 Javascript版本 --- problems/0404.左叶子之和.md | 65 ++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/problems/0404.左叶子之和.md b/problems/0404.左叶子之和.md index e5daa9db..e6a6682b 100644 --- a/problems/0404.左叶子之和.md +++ b/problems/0404.左叶子之和.md @@ -226,6 +226,71 @@ class Solution: ``` Go: +> 递归法 + +```go +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ +func sumOfLeftLeaves(root *TreeNode) int { + var res int + findLeft(root,&res) + return res +} +func findLeft(root *TreeNode,res *int){ + //左节点 + if root.Left!=nil&&root.Left.Left==nil&&root.Left.Right==nil{ + *res=*res+root.Left.Val + } + if root.Left!=nil{ + findLeft(root.Left,res) + } + if root.Right!=nil{ + findLeft(root.Right,res) + } +} +``` + +> 迭代法 + +```go +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ +func sumOfLeftLeaves(root *TreeNode) int { + var res int + queue:=list.New() + queue.PushBack(root) + for queue.Len()>0{ + length:=queue.Len() + for i:=0;i Date: Sat, 12 Jun 2021 21:55:15 +0800 Subject: [PATCH 02/13] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20513.=E6=89=BE?= =?UTF-8?q?=E6=A0=91=E5=B7=A6=E4=B8=8B=E8=A7=92=E7=9A=84=E5=80=BC=20go?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 513.找树左下角的值 go版本 --- problems/0513.找树左下角的值.md | 74 ++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/problems/0513.找树左下角的值.md b/problems/0513.找树左下角的值.md index 97464e3d..e252ccd3 100644 --- a/problems/0513.找树左下角的值.md +++ b/problems/0513.找树左下角的值.md @@ -298,6 +298,80 @@ class Solution: ``` Go: +> 递归法 + +```go +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ + var maxDeep int // 全局变量 深度 + var value int //全局变量 最终值 +func findBottomLeftValue(root *TreeNode) int { + if root.Left==nil&&root.Right==nil{//需要提前判断一下(不要这个if的话提交结果会出错,但执行代码不会。防止这种情况出现,故先判断是否只有一个节点) + return root.Val + } + findLeftValue (root,maxDeep) + return value +} +func findLeftValue (root *TreeNode,deep int){ + //最左边的值在左边 + if root.Left==nil&&root.Right==nil{ + if deep>maxDeep{ + value=root.Val + maxDeep=deep + } + } + //递归 + if root.Left!=nil{ + deep++ + findLeftValue(root.Left,deep) + deep--//回溯 + } + if root.Right!=nil{ + deep++ + findLeftValue(root.Right,deep) + deep--//回溯 + } +} +``` + +> 迭代法 + +```go +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ +func findBottomLeftValue(root *TreeNode) int { + queue:=list.New() + var gradation int + queue.PushBack(root) + for queue.Len()>0{ + length:=queue.Len() + for i:=0;i Date: Sat, 12 Jun 2021 21:57:47 +0800 Subject: [PATCH 03/13] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20404.=E5=B7=A6?= =?UTF-8?q?=E5=8F=B6=E5=AD=90=E4=B9=8B=E5=92=8C=20GO=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 404.左叶子之和 GO版本 --- problems/0404.左叶子之和.md | 1 + 1 file changed, 1 insertion(+) diff --git a/problems/0404.左叶子之和.md b/problems/0404.左叶子之和.md index e6a6682b..aa758367 100644 --- a/problems/0404.左叶子之和.md +++ b/problems/0404.左叶子之和.md @@ -340,6 +340,7 @@ var sumOfLeftLeaves = function(root) { + ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) From 93b8dda7ca2567d11198c2dd04df48864f39d645 Mon Sep 17 00:00:00 2001 From: X-shuffle <53906918+X-shuffle@users.noreply.github.com> Date: Sat, 12 Jun 2021 23:34:37 +0800 Subject: [PATCH 04/13] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200112.=E8=B7=AF?= =?UTF-8?q?=E5=BE=84=E6=80=BB=E5=92=8C=20go=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0112.路径总和.md | 86 +++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/problems/0112.路径总和.md b/problems/0112.路径总和.md index d810a046..54f79d1d 100644 --- a/problems/0112.路径总和.md +++ b/problems/0112.路径总和.md @@ -486,6 +486,92 @@ class Solution: Go: +> 112. 路径总和 + +```go +//递归法 +/** + * 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 + } + 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) + } +} +``` + + + +> 113 递归法 + +```go +/** + * 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) + 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) + } + } + 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]//回溯 + } +} +``` + JavaScript: 0112.路径总和 From e445b35d3691edf9ec0ab9fe30aaa6357b3c518a Mon Sep 17 00:00:00 2001 From: NevS <1173325467@qq.com> Date: Sun, 13 Jun 2021 01:55:37 +0800 Subject: [PATCH 05/13] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200239.=E6=BB=91?= =?UTF-8?q?=E5=8A=A8=E7=AA=97=E5=8F=A3=E6=9C=80=E5=A4=A7=E5=80=BC=20go?= =?UTF-8?q?=E7=89=88=20=EF=BC=88=E5=A2=9E=E5=8A=A0=E5=B0=81=E8=A3=85?= =?UTF-8?q?=E5=8D=95=E8=B0=83=E9=98=9F=E5=88=97=E8=A7=A3=E9=A2=98=E7=9A=84?= =?UTF-8?q?=E6=96=B9=E5=BC=8F=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加封装单调队列解题的方式 --- problems/0239.滑动窗口最大值.md | 60 ++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/problems/0239.滑动窗口最大值.md b/problems/0239.滑动窗口最大值.md index 47383a47..d108335b 100644 --- a/problems/0239.滑动窗口最大值.md +++ b/problems/0239.滑动窗口最大值.md @@ -332,6 +332,66 @@ func maxSlidingWindow(nums []int, k int) []int { ``` +```go +// 封装单调队列的方式解题 +type MyQueue struct { + queue []int +} + +func NewMyQueue() *MyQueue { + return &MyQueue{ + queue: make([]int, 0), + } +} + +func (m *MyQueue) Front() int { + return m.queue[0] +} + +func (m *MyQueue) Back() int { + return m.queue[len(m.queue)-1] +} + +func (m *MyQueue) Empty() bool { + return len(m.queue) == 0 +} + +func (m *MyQueue) Push(val int) { + for !m.Empty() && val > m.Back() { + m.queue = m.queue[:len(m.queue)-1] + } + m.queue = append(m.queue, val) +} + +func (m *MyQueue) Pop(val int) { + if !m.Empty() && val == m.Front() { + m.queue = m.queue[1:] + } +} + +func maxSlidingWindow(nums []int, k int) []int { + queue := NewMyQueue() + length := len(nums) + res := make([]int, 0) + // 先将前k个元素放入队列 + for i := 0; i < k; i++ { + queue.Push(nums[i]) + } + // 记录前k个元素的最大值 + res = append(res, queue.Front()) + + for i := k; i < length; i++ { + // 滑动窗口移除最前面的元素 + queue.Pop(nums[i-k]) + // 滑动窗口添加最后面的元素 + queue.Push(nums[i]) + // 记录最大值 + res = append(res, queue.Front()) + } + return res +} +``` + Javascript: ```javascript var maxSlidingWindow = function (nums, k) { From f5b8f8faeb8aa5965e534ba41ae8a2ea378d20b2 Mon Sep 17 00:00:00 2001 From: Kelvin Date: Sat, 12 Jun 2021 13:57:42 -0400 Subject: [PATCH 06/13] =?UTF-8?q?=E6=9B=B4=E6=96=B00343.=E6=95=B4=E6=95=B0?= =?UTF-8?q?=E6=8B=86=E5=88=86.md=20python3=E4=BB=A3=E7=A0=81=20-=20?= =?UTF-8?q?=E5=B0=8F=E6=94=B9=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 根据讲解, dp[0], dp[1]不应该初始化. 如果 是for j in range(1, i): 递归方程就会需要dp[1]. 虽然也可以AC, 但是不合逻辑. --- problems/0343.整数拆分.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0343.整数拆分.md b/problems/0343.整数拆分.md index fefaa293..cf60575f 100644 --- a/problems/0343.整数拆分.md +++ b/problems/0343.整数拆分.md @@ -218,7 +218,7 @@ class Solution: # 假设对正整数 i 拆分出的第一个正整数是 j(1 <= j < i),则有以下两种方案: # 1) 将 i 拆分成 j 和 i−j 的和,且 i−j 不再拆分成多个正整数,此时的乘积是 j * (i-j) # 2) 将 i 拆分成 j 和 i−j 的和,且 i−j 继续拆分成多个正整数,此时的乘积是 j * dp[i-j] - for j in range(1, i): + for j in range(1, i - 1): dp[i] = max(dp[i], max(j * (i - j), j * dp[i - j])) return dp[n] ``` From 6673c56916ac17ed48b7a30bffe3d8912773a0b1 Mon Sep 17 00:00:00 2001 From: Kelvin Date: Sat, 12 Jun 2021 14:12:08 -0400 Subject: [PATCH 07/13] =?UTF-8?q?Update=20=E8=83=8C=E5=8C=85=E7=90=86?= =?UTF-8?q?=E8=AE=BA=E5=9F=BA=E7=A1=8001=E8=83=8C=E5=8C=85-1.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改indention. --- problems/背包理论基础01背包-1.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/背包理论基础01背包-1.md b/problems/背包理论基础01背包-1.md index 852c489b..36544256 100644 --- a/problems/背包理论基础01背包-1.md +++ b/problems/背包理论基础01背包-1.md @@ -268,7 +268,7 @@ int main() { Java: ```java - public static void main(String[] args) { + public static void main(String[] args) { int[] weight = {1, 3, 4}; int[] value = {15, 20, 30}; int bagSize = 4; From 6a42937927f1e9c9c4925fa53332ddf4fe275d24 Mon Sep 17 00:00:00 2001 From: Kelvin Date: Sat, 12 Jun 2021 14:29:59 -0400 Subject: [PATCH 08/13] =?UTF-8?q?Update=20=E8=83=8C=E5=8C=85=E7=90=86?= =?UTF-8?q?=E8=AE=BA=E5=9F=BA=E7=A1=8001=E8=83=8C=E5=8C=85-2.md=20Python?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加python部分代码. 测试结果: ``` [[0, 15, 15, 15, 15], [0, 15, 15, 20, 35], [0, 15, 15, 20, 35]] ``` 与题解一致. --- problems/背包理论基础01背包-1.md | 35 ++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/problems/背包理论基础01背包-1.md b/problems/背包理论基础01背包-1.md index 36544256..1269d9c1 100644 --- a/problems/背包理论基础01背包-1.md +++ b/problems/背包理论基础01背包-1.md @@ -307,6 +307,41 @@ Java: Python: +```python +def test_2_wei_bag_problem1(bag_size, weight, value) -> int: + rows, cols = len(weight), bag_size + 1 + dp = [[0 for _ in range(cols)] for _ in range(rows)] + res = 0 + + # 初始化dp数组. + for i in range(rows): + dp[i][0] = 0 + first_item_weight, first_item_value = weight[0], value[0] + for j in range(1, cols): + if first_item_weight <= j: + dp[0][j] = first_item_value + + # 更新dp数组: 先遍历物品, 再遍历背包. + for i in range(1, len(weight)): + cur_weight, cur_val = weight[i], value[i] + for j in range(1, cols): + if cur_weight > j: # 说明背包装不下当前物品. + dp[i][j] = dp[i - 1][j] # 所以不装当前物品. + else: + # 定义dp数组: dp[i][j] 前i个物品里,放进容量为j的背包,价值总和最大是多少。 + dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - cur_weight]+ cur_val) + if dp[i][j] > res: + res = dp[i][j] + + print(dp) + + +if __name__ == "__main__": + bag_size = 4 + weight = [1, 3, 4] + value = [15, 20, 30] + test_2_wei_bag_problem1(bag_size, weight, value) +``` Go: From be2804f8740e5027354265d722d77e9e89e7fd0d Mon Sep 17 00:00:00 2001 From: Kelvin Date: Sat, 12 Jun 2021 14:30:30 -0400 Subject: [PATCH 09/13] =?UTF-8?q?Revert=20"Update=20=E8=83=8C=E5=8C=85?= =?UTF-8?q?=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=8001=E8=83=8C=E5=8C=85-1.md"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit 6673c56916ac17ed48b7a30bffe3d8912773a0b1. --- problems/背包理论基础01背包-1.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/背包理论基础01背包-1.md b/problems/背包理论基础01背包-1.md index 1269d9c1..f6646202 100644 --- a/problems/背包理论基础01背包-1.md +++ b/problems/背包理论基础01背包-1.md @@ -268,7 +268,7 @@ int main() { Java: ```java - public static void main(String[] args) { + public static void main(String[] args) { int[] weight = {1, 3, 4}; int[] value = {15, 20, 30}; int bagSize = 4; From 737b7c38e86a1365b99f4bcc4ef354c04dd44955 Mon Sep 17 00:00:00 2001 From: Kelvin Date: Sat, 12 Jun 2021 14:32:08 -0400 Subject: [PATCH 10/13] =?UTF-8?q?=E6=9B=B4=E6=94=B9=20=E8=83=8C=E5=8C=85?= =?UTF-8?q?=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=8001=E8=83=8C=E5=8C=85-2.md?= =?UTF-8?q?=20Java=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 原版在github上显示不正确. --- problems/背包理论基础01背包-1.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/背包理论基础01背包-1.md b/problems/背包理论基础01背包-1.md index f6646202..1269d9c1 100644 --- a/problems/背包理论基础01背包-1.md +++ b/problems/背包理论基础01背包-1.md @@ -268,7 +268,7 @@ int main() { Java: ```java - public static void main(String[] args) { + public static void main(String[] args) { int[] weight = {1, 3, 4}; int[] value = {15, 20, 30}; int bagSize = 4; From 3e506df04426ca8937f09f7a8e9ca9955d0d1c7e Mon Sep 17 00:00:00 2001 From: borninfreedom Date: Sun, 13 Jun 2021 10:56:21 +0800 Subject: [PATCH 11/13] =?UTF-8?q?update=E6=9C=89=E6=95=88=E7=9A=84?= =?UTF-8?q?=E5=AD=97=E6=AF=8D=E5=BC=82=E4=BD=8D=E8=AF=8D=EF=BC=8Cpython?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E6=B7=BB=E5=8A=A0=E4=BA=86=E6=9B=B4pythonic?= =?UTF-8?q?=E7=9A=84=E5=86=99=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0242.有效的字母异位词.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/problems/0242.有效的字母异位词.md b/problems/0242.有效的字母异位词.md index ba942f70..c3e73730 100644 --- a/problems/0242.有效的字母异位词.md +++ b/problems/0242.有效的字母异位词.md @@ -130,7 +130,27 @@ class Solution: return True ``` +Python写法二: + +```python +class Solution: + def isAnagram(self, s: str, t: str) -> bool: + from collections import defaultdict + + s_dict=defaultdict(int) + t_dict=defaultdict(int) + + for x in s: + s_dict[x]+=1 + + for x in t: + t_dict[x]+=1 + + return s_dict==t_dict +``` + Go: + ```go func isAnagram(s string, t string) bool { if len(s)!=len(t){ From 0d3b8de51227c0c55ace89aa249e0a25377a62be Mon Sep 17 00:00:00 2001 From: borninfreedom Date: Sun, 13 Jun 2021 10:58:05 +0800 Subject: [PATCH 12/13] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E8=A7=84=E8=8C=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0242.有效的字母异位词.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/problems/0242.有效的字母异位词.md b/problems/0242.有效的字母异位词.md index c3e73730..1956253a 100644 --- a/problems/0242.有效的字母异位词.md +++ b/problems/0242.有效的字母异位词.md @@ -137,16 +137,16 @@ class Solution: def isAnagram(self, s: str, t: str) -> bool: from collections import defaultdict - s_dict=defaultdict(int) - t_dict=defaultdict(int) + s_dict = defaultdict(int) + t_dict = defaultdict(int) for x in s: - s_dict[x]+=1 + s_dict[x] += 1 for x in t: - t_dict[x]+=1 + t_dict[x] += 1 - return s_dict==t_dict + return s_dict == t_dict ``` Go: From 4993e9ff6bfab26473eeb9b99f07ae544f0afca3 Mon Sep 17 00:00:00 2001 From: borninfreedom Date: Sun, 13 Jun 2021 11:14:21 +0800 Subject: [PATCH 13/13] =?UTF-8?q?=E6=9C=89=E6=95=88=E7=9A=84=E5=AD=97?= =?UTF-8?q?=E6=AF=8D=E5=BC=82=E4=BD=8D=E8=AF=8D=E6=B7=BB=E5=8A=A0=E4=B8=80?= =?UTF-8?q?=E4=BA=9B=E8=AF=B4=E6=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0242.有效的字母异位词.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0242.有效的字母异位词.md b/problems/0242.有效的字母异位词.md index 1956253a..93bba44c 100644 --- a/problems/0242.有效的字母异位词.md +++ b/problems/0242.有效的字母异位词.md @@ -130,7 +130,7 @@ class Solution: return True ``` -Python写法二: +Python写法二(没有使用数组作为哈希表,只是介绍defaultdict这样一种解题思路): ```python class Solution: