组合总和 II
- 难度: 中等
- 通过率: 39.2%
- 题目链接:https://leetcode-cn.com/problems/combination-sum-ii
题目描述
给定一个数组 candidates
和一个目标数 target
,找出 candidates
中所有可以使数字和为 target
的组合。
candidates
中的每个数字在每个组合中只能使用一次。
说明:
- 所有数字(包括目标数)都是正整数。
- 解集不能包含重复的组合。
示例 1:
输入: candidates =[10,1,2,7,6,1,5]
, target =8
, 所求解集为: [ [1, 7], [1, 2, 5], [2, 6], [1, 1, 6] ]
示例 2:
输入: candidates = [2,5,2,1,2], target = 5, 所求解集为: [ [1,2,2], [5] ]
解法:
本题和 39 题 组合总和 的区别在于本题中存在重复元素,且同一个元素不能多次使用。如果采用 39 题的解法,会出现重复。
此处只需要在 39 的基础上稍做改动即可。
1. 跳过上次使用的数字:
auto it = candidates.begin() + start + 1;
2. 不使用重复元素:
it = upper_bound(candidates.begin(), candidates.end(), *it);
3. dfs
的第三个参数 start
指向下一个元素。
class Solution {
public:
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
sort(candidates.begin(), candidates.end());
vector<vector<int>> result;
vector<int> path; // 中间结果
dfs(candidates, path, result, 0, target);
return result;
}
void dfs(const vector<int>& candidates, vector<int>& path,
vector<vector<int>>& result, size_t start, int target){
if(target == 0){
result.push_back(path);
}
auto it = candidates.begin() + start;
while(it != candidates.end()){
if(target < *it){
return;
}
path.push_back(*it);
dfs(candidates, path, result, it - candidates.begin() + 1, target - *it);
path.pop_back();
it = upper_bound(candidates.begin(), candidates.end(), *it);
}
}
};