Leetcode 40
Combination Sum II
class Solution {
public:
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
vector<int> t;
vector<vector<int>> res;
if (candidates.size() == 0) return res;
sort(candidates.begin(), candidates.end());
vector<int> p;
combination(p, candidates, res, target, 0);
return res;
}
void combination(vector<int>& p, vector<int>& candidates, vector<vector<int>>& res, int target, int i) {
if (target<0) {
return;
} else if (target==0) {
res.push_back(p);
return;
} else {
for (int j = i; j < candidates.size(); j++) {
if (j > i && candidates[j] == candidates[j-1]) continue;
p.push_back(candidates[j]);
combination(p, candidates, res, target-candidates[j], j+1);
p.erase(p.begin()+p.size()-1);
}
}
}
};