@@ -69,6 +69,31 @@ void combineDFS(int n, int k, int begin, vector<int> &path,
|
||||
}
|
||||
```
|
||||
|
||||
## [216. 组合总和 III](https://leetcode.cn/problems/combination-sum-iii/)
|
||||
|
||||
## [39. 组合总和](https://leetcode.cn/problems/combination-sum/)
|
||||
|
||||
## [216. 组合总和 III](https://leetcode.cn/problems/combination-sum-iii/)
|
||||
## [40. 组合总和 II](https://leetcode.cn/problems/combination-sum-ii/)
|
||||
|
||||
最难的一个组合总和,因为 `candidates` 有重复元素,而要求最终结果不能重复。
|
||||
|
||||
e.g. 1
|
||||
|
||||
```text
|
||||
Input: candidates = [10,1,2,7,6,1,5], target = 8
|
||||
Output:
|
||||
[
|
||||
[1,1,6],
|
||||
[1,2,5],
|
||||
[1,7],
|
||||
[2,6]
|
||||
]
|
||||
```
|
||||
|
||||
如果你只是单纯地在 s0039 的基础上在下一次递归中将 `startIndex` 设为 `i + 1` 那么最终结果就会出现两个 `[1, 2, 5]`。
|
||||
|
||||
如果你直接排除 `candidates[i] == candidates[i - 1]` 的情形,那么最终结果就没有 `[1, 1, 6]`。
|
||||
|
||||
正确的逻辑应该是如果 `candidates[i] == candidates[i - 1]` 且 `candidates[i - 1]` 使用过,则剪枝。
|
||||
|
||||
怎么判断 `candidates[i - 1]` 是否使用过呢?我们创建一个 `vector<bool> used` 用来记录每个元素是否使用过。
|
||||
|
Reference in New Issue
Block a user