From 417d5b80bfb58654c13b70c08d83d2a22add3a61 Mon Sep 17 00:00:00 2001 From: ArthurP Date: Sat, 28 Aug 2021 22:56:15 +0800 Subject: [PATCH 01/10] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200134.=E5=8A=A0?= =?UTF-8?q?=E6=B2=B9=E7=AB=99.md=20C=E8=AF=AD=E8=A8=80=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0134.加油站.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/problems/0134.加油站.md b/problems/0134.加油站.md index 0befd085..ff54fbc6 100644 --- a/problems/0134.加油站.md +++ b/problems/0134.加油站.md @@ -283,6 +283,30 @@ var canCompleteCircuit = function(gas, cost) { }; ``` +C: +```c +int canCompleteCircuit(int* gas, int gasSize, int* cost, int costSize){ + int curSum = 0; + int i; + int min = INT_MAX; + for(i = 0; i < gasSize; i++) { + int diff = gas[i] - cost[i]; + curSum += diff; + if(min > curSum) + min = curSum; + } + if(curSum < 0) + return -1; + if(min >= 0) + return 0; + for(i = gasSize - 1; i >= 0; i--) { + min+=(gas[i]-cost[i]); + if(min >= 0) + return i; + } + return 0; +} +``` ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) From 6333521adf7e6204a55dbb2c1f831429f688c4ea Mon Sep 17 00:00:00 2001 From: ArthurP Date: Sun, 29 Aug 2021 10:28:15 +0800 Subject: [PATCH 02/10] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200452.=E7=94=A8?= =?UTF-8?q?=E6=9C=80=E5=B0=91=E6=95=B0=E9=87=8F=E7=9A=84=E7=AE=AD=E5=BC=95?= =?UTF-8?q?=E7=88=86=E6=B0=94=E7=90=83.md=20C=E8=AF=AD=E8=A8=80=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0452.用最少数量的箭引爆气球.md | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/problems/0452.用最少数量的箭引爆气球.md b/problems/0452.用最少数量的箭引爆气球.md index 7b8130f0..a2168dfc 100644 --- a/problems/0452.用最少数量的箭引爆气球.md +++ b/problems/0452.用最少数量的箭引爆气球.md @@ -218,6 +218,30 @@ var findMinArrowShots = function(points) { }; ``` +C: +```c +int cmp(const void *a,const void *b) +{ + return ((*((int**)a))[0] > (*((int**)b))[0]); +} + +int findMinArrowShots(int** points, int pointsSize, int* pointsColSize){ + //将points数组作升序排序 + qsort(points, pointsSize, sizeof(points[0]),cmp); + + int arrowNum = 1; + int i = 1; + for(i = 1; i < pointsSize; i++) { + //若前一个气球与当前气球不重叠,证明需要增加箭的数量 + if(points[i][0] > points[i-1][1]) + arrowNum++; + else + //若前一个气球与当前气球重叠,判断并最小的x_end + points[i][1] = points[i][1] > points[i-1][1] ? points[i-1][1] : points[i][1]; + } + return arrowNum; +} +``` ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) From d327806115b66777e8e19ee27af10d4491b7a609 Mon Sep 17 00:00:00 2001 From: ArthurP Date: Sun, 29 Aug 2021 10:37:39 +0800 Subject: [PATCH 03/10] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200134.=E5=8A=A0?= =?UTF-8?q?=E6=B2=B9=E7=AB=99.md=20C=E8=AF=AD=E8=A8=80=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0134.加油站.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/problems/0134.加油站.md b/problems/0134.加油站.md index ff54fbc6..ff3bc8b2 100644 --- a/problems/0134.加油站.md +++ b/problems/0134.加油站.md @@ -289,21 +289,26 @@ int canCompleteCircuit(int* gas, int gasSize, int* cost, int costSize){ int curSum = 0; int i; int min = INT_MAX; + //遍历整个数组。计算出每站的用油差。并将其与最小累加量比较 for(i = 0; i < gasSize; i++) { int diff = gas[i] - cost[i]; curSum += diff; - if(min > curSum) + if(curSum < min) min = curSum; } + //若汽油总数为负数,代表无法跑完一环。返回-1 if(curSum < 0) return -1; + //若min大于等于0,说明每一天加油量比用油量多。因此从0出发即可 if(min >= 0) return 0; + //若累加最小值为负,则找到一个非零元素(加油量大于出油量)出发。返回坐标 for(i = gasSize - 1; i >= 0; i--) { min+=(gas[i]-cost[i]); if(min >= 0) return i; } + //逻辑上不会返回这个0 return 0; } ``` From cbdc4bd369541f15f504d71c4203a452a7e9ca65 Mon Sep 17 00:00:00 2001 From: ArthurP Date: Sun, 29 Aug 2021 10:40:41 +0800 Subject: [PATCH 04/10] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200452.=E7=94=A8?= =?UTF-8?q?=E6=9C=80=E5=B0=91=E6=95=B0=E9=87=8F=E7=9A=84=E7=AE=AD=E5=BC=95?= =?UTF-8?q?=E7=88=86=E6=B0=94=E7=90=83.md=20C=E8=AF=AD=E8=A8=80=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0452.用最少数量的箭引爆气球.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0452.用最少数量的箭引爆气球.md b/problems/0452.用最少数量的箭引爆气球.md index a2168dfc..07141558 100644 --- a/problems/0452.用最少数量的箭引爆气球.md +++ b/problems/0452.用最少数量的箭引爆气球.md @@ -236,7 +236,7 @@ int findMinArrowShots(int** points, int pointsSize, int* pointsColSize){ if(points[i][0] > points[i-1][1]) arrowNum++; else - //若前一个气球与当前气球重叠,判断并最小的x_end + //若前一个气球与当前气球重叠,判断并更新最小的x_end points[i][1] = points[i][1] > points[i-1][1] ? points[i-1][1] : points[i][1]; } return arrowNum; From b4858a21cdf7a154e84fd3825b031740a7a6db08 Mon Sep 17 00:00:00 2001 From: ironartisan Date: Sun, 29 Aug 2021 10:58:53 +0800 Subject: [PATCH 05/10] =?UTF-8?q?=E4=BF=AE=E5=A4=8D1005.K=E5=8F=96?= =?UTF-8?q?=E5=8F=8D=E5=90=8E=E6=9C=80=E5=A4=A7=E5=8C=96=E7=9A=84=E6=95=B0?= =?UTF-8?q?=E7=BB=84=E5=92=8CJava=E4=BB=A3=E7=A0=81K=E4=B9=A6=E5=86=99?= =?UTF-8?q?=E9=94=99=E8=AF=AF=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/1005.K次取反后最大化的数组和.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/problems/1005.K次取反后最大化的数组和.md b/problems/1005.K次取反后最大化的数组和.md index 8bdd0f41..b9973b5f 100644 --- a/problems/1005.K次取反后最大化的数组和.md +++ b/problems/1005.K次取反后最大化的数组和.md @@ -110,13 +110,13 @@ class Solution { int len = nums.length; for (int i = 0; i < len; i++) { //从前向后遍历,遇到负数将其变为正数,同时K-- - if (nums[i] < 0 && k > 0) { + if (nums[i] < 0 && K > 0) { nums[i] = -nums[i]; - k--; + K--; } } // 如果K还大于0,那么反复转变数值最小的元素,将K用完 - if (k % 2 == 1) nums[len - 1] = -nums[len - 1]; + if (K % 2 == 1) nums[len - 1] = -nums[len - 1]; int result = 0; for (int a : nums) { result += a; From 051c2c5c3e6e77c757ffb94ba2e8f24f21243cc2 Mon Sep 17 00:00:00 2001 From: ironartisan Date: Sun, 29 Aug 2021 11:02:52 +0800 Subject: [PATCH 06/10] =?UTF-8?q?=E4=BF=AE=E5=A4=8D1005.K=E5=8F=96?= =?UTF-8?q?=E5=8F=8D=E5=90=8E=E6=9C=80=E5=A4=A7=E5=8C=96=E7=9A=84=E6=95=B0?= =?UTF-8?q?=E7=BB=84=E5=92=8CJava=E4=BB=A3=E7=A0=81K=E4=B9=A6=E5=86=99?= =?UTF-8?q?=E9=94=99=E8=AF=AF=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/1005.K次取反后最大化的数组和.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/1005.K次取反后最大化的数组和.md b/problems/1005.K次取反后最大化的数组和.md index b9973b5f..0a0c8d62 100644 --- a/problems/1005.K次取反后最大化的数组和.md +++ b/problems/1005.K次取反后最大化的数组和.md @@ -112,7 +112,7 @@ class Solution { //从前向后遍历,遇到负数将其变为正数,同时K-- if (nums[i] < 0 && K > 0) { nums[i] = -nums[i]; - K--; + K--; } } // 如果K还大于0,那么反复转变数值最小的元素,将K用完 From 615139b74d11f47116b8b1e08e04936ba6184d72 Mon Sep 17 00:00:00 2001 From: ironartisan Date: Sun, 29 Aug 2021 18:55:36 +0800 Subject: [PATCH 07/10] =?UTF-8?q?=E6=B7=BB=E5=8A=A00046.=E5=85=A8=E6=8E=92?= =?UTF-8?q?=E5=88=97=E6=96=B0=E8=A7=A3=E6=B3=95Java=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0046.全排列.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/problems/0046.全排列.md b/problems/0046.全排列.md index df9394eb..001c249e 100644 --- a/problems/0046.全排列.md +++ b/problems/0046.全排列.md @@ -183,6 +183,32 @@ class Solution { } } ``` +```java +// 解法2:通过判断path中是否存在数字,排除已经选择的数字 +class Solution { + List> result = new ArrayList<>(); + LinkedList path = new LinkedList<>(); + public List> permute(int[] nums) { + if (nums.length == 0) return result; + backtrack(nums, path); + return result; + } + public void backtrack(int[] nums, LinkedList path) { + if (path.size() == nums.length) { + result.add(new ArrayList<>(path)); + } + for (int i =0; i < nums.length; i++) { + // 如果path中已有,则跳过 + if (path.contains(nums[i])) { + continue; + } + path.add(nums[i]); + backtrack(nums, path); + path.removeLast(); + } + } +} +``` Python: ```python3 From eb17c57bf3c0c3c5785568614073800866770f3f Mon Sep 17 00:00:00 2001 From: ironartisan Date: Mon, 30 Aug 2021 10:23:08 +0800 Subject: [PATCH 08/10] =?UTF-8?q?=E5=A2=9E=E5=8A=A00134.=E5=8A=A0=E6=B2=B9?= =?UTF-8?q?=E7=AB=99Java=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0134.加油站.md | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/problems/0134.加油站.md b/problems/0134.加油站.md index 0befd085..bee909c3 100644 --- a/problems/0134.加油站.md +++ b/problems/0134.加油站.md @@ -200,6 +200,7 @@ public: Java: ```java +// 解法1 class Solution { public int canCompleteCircuit(int[] gas, int[] cost) { int sum = 0; @@ -221,7 +222,26 @@ class Solution { } } ``` - +```java +// 解法2 +class Solution { + public int canCompleteCircuit(int[] gas, int[] cost) { + int curSum = 0; + int totalSum = 0; + int index = 0; + for (int i = 0; i < gas.length; i++) { + curSum += gas[i] - cost[i]; + totalSum += gas[i] - cost[i]; + if (curSum < 0) { + index = (i + 1) % gas.length ; + curSum = 0; + } + } + if (totalSum < 0) return -1; + return index; + } +} +``` Python: ```python class Solution: From ee6bd7413c7a94135c760363c8a5f3fb6ccb0085 Mon Sep 17 00:00:00 2001 From: ironartisan Date: Tue, 31 Aug 2021 10:46:18 +0800 Subject: [PATCH 09/10] =?UTF-8?q?=E6=B7=BB=E5=8A=A00056.=E5=90=88=E5=B9=B6?= =?UTF-8?q?=E5=8C=BA=E9=97=B4Java=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0056.合并区间.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/problems/0056.合并区间.md b/problems/0056.合并区间.md index 2322951a..82ca29e6 100644 --- a/problems/0056.合并区间.md +++ b/problems/0056.合并区间.md @@ -157,6 +157,28 @@ class Solution { } } ``` +```java +// 版本2 +class Solution { + public int[][] merge(int[][] intervals) { + LinkedList res = new LinkedList<>(); + Arrays.sort(intervals, (o1, o2) -> Integer.compare(o1[0], o2[0])); + res.add(intervals[0]); + for (int i = 1; i < intervals.length; i++) { + if (intervals[i][0] <= res.getLast()[1]) { + int start = res.getLast()[0]; + int end = Math.max(intervals[i][1], res.getLast()[1]); + res.removeLast(); + res.add(new int[]{start, end}); + } + else { + res.add(intervals[i]); + } + } + return res.toArray(new int[res.size()][]); + } +} +``` Python: ```python From 09002bc5388d1d604f263e71aebe9a65e8dd6cac Mon Sep 17 00:00:00 2001 From: youngyangyang04 <826123027@qq.com> Date: Tue, 31 Aug 2021 10:47:26 +0800 Subject: [PATCH 10/10] Update --- ...35.二叉搜索树的最近公共祖先.md | 96 +++++++------------ problems/背包理论基础01背包-1.md | 2 +- 2 files changed, 37 insertions(+), 61 deletions(-) diff --git a/problems/0235.二叉搜索树的最近公共祖先.md b/problems/0235.二叉搜索树的最近公共祖先.md index fab450ba..929e6eb2 100644 --- a/problems/0235.二叉搜索树的最近公共祖先.md +++ b/problems/0235.二叉搜索树的最近公共祖先.md @@ -7,7 +7,7 @@

欢迎大家参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

-## 235. 二叉搜索树的最近公共祖先 +# 235. 二叉搜索树的最近公共祖先 [力扣题目链接](https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-search-tree/) @@ -21,14 +21,15 @@ 示例 1: -输入: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 8 -输出: 6 -解释: 节点 2 和节点 8 的最近公共祖先是 6。 +* 输入: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 8 +* 输出: 6 +* 解释: 节点 2 和节点 8 的最近公共祖先是 6。 + 示例 2: -输入: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 4 -输出: 2 -解释: 节点 2 和节点 4 的最近公共祖先是 2, 因为根据定义最近公共祖先节点可以为节点本身。 +* 输入: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 4 +* 输出: 2 +* 解释: 节点 2 和节点 4 的最近公共祖先是 2, 因为根据定义最近公共祖先节点可以为节点本身。 说明: @@ -36,7 +37,9 @@ * 所有节点的值都是唯一的。 * p、q 为不同节点且均存在于给定的二叉搜索树中。 -## 思路 +# 思路 + + 做过[二叉树:公共祖先问题](https://programmercarl.com/0236.二叉树的最近公共祖先.html)题目的同学应该知道,利用回溯从底向上搜索,遇到一个节点的左子树里有p,右子树里有q,那么当前节点就是最近公共祖先。 @@ -58,6 +61,7 @@ 可以看出直接按照指定的方向,就可以找到节点4,为最近公共祖先,而且不需要遍历整棵树,找到结果直接返回! +## 递归法 递归三部曲如下: @@ -111,7 +115,6 @@ if (cur->val > p->val && cur->val > q->val) { ``` if (递归函数(root->left)) return ; - if (递归函数(root->right)) return ; ``` @@ -128,7 +131,7 @@ left与right的逻辑处理; 如果 cur->val 小于 p->val,同时 cur->val 小于 q->val,那么就应该向右遍历(目标区间在右子树)。 -``` +```CPP if (cur->val < p->val && cur->val < q->val) { TreeNode* right = traversal(cur->right, p, q); if (right != NULL) { @@ -140,9 +143,9 @@ if (cur->val < p->val && cur->val < q->val) { 剩下的情况,就是cur节点在区间(p->val <= cur->val && cur->val <= q->val)或者 (q->val <= cur->val && cur->val <= p->val)中,那么cur就是最近公共祖先了,直接返回cur。 代码如下: + ``` return cur; - ``` 那么整体递归代码如下: @@ -216,7 +219,7 @@ public: 灵魂拷问:是不是又被简单的迭代法感动到痛哭流涕? -## 总结 +# 总结 对于二叉搜索树的最近祖先问题,其实要比[普通二叉树公共祖先问题](https://programmercarl.com/0236.二叉树的最近公共祖先.html)简单的多。 @@ -225,10 +228,15 @@ public: 最后给出了对应的迭代法,二叉搜索树的迭代法甚至比递归更容易理解,也是因为其有序性(自带方向性),按照目标区间找就行了。 -## 其他语言版本 +# 其他语言版本 -Java: +## Java + +递归法: + + +迭代法: ```java class Solution { public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { @@ -246,15 +254,11 @@ class Solution { } ``` -Python: -```python -# Definition for a binary tree node. -# class TreeNode: -# def __init__(self, x): -# self.val = x -# self.left = None -# self.right = None +## Python + +递归法: +```python class Solution: def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode': if not root: return root //中 @@ -264,18 +268,14 @@ class Solution: return self.lowestCommonAncestor(root.right,p,q) //右 else: return root ``` -Go: -> BSL法 +迭代法: + + +## Go + +递归法: ```go -/** - * Definition for a binary tree node. - * type TreeNode struct { - * Val int - * Left *TreeNode - * Right *TreeNode - * } - */ //利用BSL的性质(前序遍历有序) func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode { if root==nil{return nil} @@ -287,34 +287,10 @@ func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode { } ``` -> 普通法 -```go -/** - * Definition for a binary tree node. - * type TreeNode struct { - * Val int - * Left *TreeNode - * Right *TreeNode - * } - */ -//递归会将值层层返回 -func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode { - //终止条件 - if root==nil||root.Val==p.Val||root.Val==q.Val{return root}//最后为空或者找到一个值时,就返回这个值 - //后序遍历 - findLeft:=lowestCommonAncestor(root.Left,p,q) - findRight:=lowestCommonAncestor(root.Right,p,q) - //处理单层逻辑 - if findLeft!=nil&&findRight!=nil{return root}//说明在root节点的两边 - if findLeft==nil{//左边没找到,就说明在右边找到了 - return findRight - }else {return findLeft} -} -``` +## JavaScript -JavaScript版本: -1. 使用递归的方法 +递归法: ```javascript var lowestCommonAncestor = function(root, p, q) { // 使用递归的方法 @@ -336,7 +312,8 @@ var lowestCommonAncestor = function(root, p, q) { return root; }; ``` -2. 使用迭代的方法 + +迭代法 ```javascript var lowestCommonAncestor = function(root, p, q) { // 使用迭代的方法 @@ -355,7 +332,6 @@ var lowestCommonAncestor = function(root, p, q) { ``` - ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) diff --git a/problems/背包理论基础01背包-1.md b/problems/背包理论基础01背包-1.md index 19432371..2bcded70 100644 --- a/problems/背包理论基础01背包-1.md +++ b/problems/背包理论基础01背包-1.md @@ -137,7 +137,7 @@ dp[0][j] 和 dp[i][0] 都已经初始化了,那么其他下标应该初始化 ``` // 初始化 dp -vector> dp(weight.size() + 1, vector(bagWeight + 1, 0)); +vector> dp(weight.size(), vector(bagWeight + 1, 0)); for (int j = weight[0]; j <= bagWeight; j++) { dp[0][j] = value[0]; }