lc40-combination sum II

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Solution {
vector<vector<int>> res;
public:
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
sort(candidates.begin(), candidates.end());
vector<int> p;
helper(p, target, candidates, 0);
return res;
}
void helper(vector<int> &partial, int target, vector<int>& candidates, int start) {
int l = candidates.size();
if (!target) res.push_back(partial);
int w;
for (int i = start; i < l; i++) {
if (w == candidates[i]) continue;
w = candidates[i];
if (w<=target) {
partial.push_back(w);
helper(partial, target-w, candidates, i+1);
}
else break;
partial.pop_back();
}
}
};