LeetCode 40 组合总和 II
作者:互联网
class Solution {
public:
vector<vector<int>> res;
vector<int> path;
int sum;
void dfs(int start, vector<int>& candidates, int target) {
if (sum > target) return;
if (sum == target) {
res.push_back(path);
return;
}
for (int i = start; i < candidates.size(); i ++) {
if (i > start && candidates[i] == candidates[i - 1]) continue; //去重,同一树层上不能使用相同的数
path.push_back(candidates[i]);
sum += candidates[i];
dfs(i + 1, candidates, target);
sum -= candidates[i];
path.pop_back();
}
}
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
sort(candidates.begin(), candidates.end());
dfs(0, candidates, target);
return res;
}
};
标签:target,int,sum,40,II,vector,candidates,path,LeetCode 来源: https://www.cnblogs.com/hjy94wo/p/16654271.html