Update
This commit is contained in:
@@ -9,7 +9,7 @@
|
||||
|
||||
> 这篇可以说是全网把组合问题如何去重,讲的最清晰的了!
|
||||
|
||||
## 40.组合总和II
|
||||
# 40.组合总和II
|
||||
|
||||
[力扣题目链接](https://leetcode-cn.com/problems/combination-sum-ii/)
|
||||
|
||||
@@ -39,7 +39,7 @@ candidates 中的每个数字在每个组合中只能使用一次。
|
||||
[5]
|
||||
]
|
||||
|
||||
## 思路
|
||||
# 思路
|
||||
|
||||
**如果对回溯算法基础还不了解的话,我还特意录制了一期视频:[带你学透回溯算法(理论篇)](https://www.bilibili.com/video/BV1cy4y167mM/)** 可以结合题解和视频一起看,希望对大家理解回溯算法有所帮助。
|
||||
|
||||
@@ -157,7 +157,6 @@ for (int i = startIndex; i < candidates.size() && sum + candidates[i] <= target;
|
||||
|
||||
**注意sum + candidates[i] <= target为剪枝操作,在[39.组合总和](https://mp.weixin.qq.com/s/FLg8G6EjVcxBjwCbzpACPw)有讲解过!**
|
||||
|
||||
## C++代码
|
||||
|
||||
回溯三部曲分析完了,整体C++代码如下:
|
||||
|
||||
@@ -242,7 +241,7 @@ public:
|
||||
|
||||
```
|
||||
|
||||
## 总结
|
||||
# 总结
|
||||
|
||||
本题同样是求组合总和,但就是因为其数组candidates有重复元素,而要求不能有重复的组合,所以相对于[39.组合总和](https://programmercarl.com/0039.组合总和.html)难度提升了不少。
|
||||
|
||||
@@ -254,10 +253,10 @@ public:
|
||||
|
||||
|
||||
|
||||
## 其他语言版本
|
||||
# 其他语言版本
|
||||
|
||||
|
||||
Java:
|
||||
## Java
|
||||
```Java
|
||||
class Solution {
|
||||
List<List<Integer>> lists = new ArrayList<>();
|
||||
@@ -295,7 +294,8 @@ class Solution {
|
||||
}
|
||||
}
|
||||
```
|
||||
Python:
|
||||
|
||||
## Python
|
||||
```python
|
||||
class Solution:
|
||||
def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]:
|
||||
@@ -315,10 +315,9 @@ class Solution:
|
||||
backtrack(candidates,target,0,0)
|
||||
return res
|
||||
```
|
||||
Go:
|
||||
|
||||
|
||||
> 主要在于如何在回溯中去重
|
||||
## Go:
|
||||
主要在于如何在回溯中去重
|
||||
|
||||
```go
|
||||
func combinationSum2(candidates []int, target int) [][]int {
|
||||
@@ -359,7 +358,8 @@ func backtracking(startIndex,sum,target int,candidates,trcak []int,res *[][]int,
|
||||
}
|
||||
}
|
||||
```
|
||||
javaScript:
|
||||
|
||||
## javaScript:
|
||||
|
||||
```js
|
||||
/**
|
||||
@@ -392,7 +392,8 @@ var combinationSum2 = function(candidates, target) {
|
||||
}
|
||||
};
|
||||
```
|
||||
C:
|
||||
## C
|
||||
|
||||
```c
|
||||
int* path;
|
||||
int pathTop;
|
||||
|
||||
Reference in New Issue
Block a user