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 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 diff --git a/problems/0134.加油站.md b/problems/0134.加油站.md index 0befd085..9aa88fce 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: @@ -283,6 +303,35 @@ 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(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; +} +``` ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) 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/0452.用最少数量的箭引爆气球.md b/problems/0452.用最少数量的箭引爆气球.md index 7b8130f0..07141558 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) diff --git a/problems/1005.K次取反后最大化的数组和.md b/problems/1005.K次取反后最大化的数组和.md index 30718b13..020476a9 100644 --- a/problems/1005.K次取反后最大化的数组和.md +++ b/problems/1005.K次取反后最大化的数组和.md @@ -110,15 +110,16 @@ 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]; return Arrays.stream(nums).sum(); + } } ``` 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]; }