-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path40.cpp
28 lines (26 loc) · 887 Bytes
/
40.cpp
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
26
27
28
class Solution {
public:
vector<vector<int>> combinationSum2(vector<int> &candidates, int target) {
sort(candidates.begin(), candidates.end());
vector<int> temp;
helper(candidates, target, 0, temp, 0);
return ret;
}
void helper(vector<int> &candidates, int &target, int sum, vector<int> &temp, int index) {
if (sum == target)
ret.push_back(temp);
if (sum >= target)
return;
for (int i = index; i < candidates.size(); ++i) {
if (sum + candidates[i] > target)
continue;
if (i == index || candidates[i] != candidates[i - 1]) {
temp.push_back(candidates[i]);
helper(candidates, target, sum + candidates[i], temp, i + 1);
temp.pop_back();
}
}
}
private:
vector<vector<int>> ret;
};