-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem40.cpp
34 lines (33 loc) · 1013 Bytes
/
problem40.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
29
30
31
32
33
34
class Solution {
public:
vector<vector<int>>ans;
vector<int>temp;
vector<vector<int>> combinationSum2(vector<int>& can, int tar) {
sort(can.begin(), can.end());
backtrack(0, tar, can);
return ans;
}
void backtrack(int i, int target, vector<int>& can){
// get an answer
if(!target)
ans.push_back(temp);
// base case
if(target <= 0 || i == can.size())
return;
// peek
temp.push_back(can[i]);
backtrack(i+1, target-can[i], can);
temp.pop_back();
// This loop avoids duplicate combinations, such as:
// For example:
// 0 1 2 3
// 1, 1, 2, 7
// 1(0), 7(3)
// 1(1), 7(3)
// If the first one is taken, don't take the next one if this combination reaches the target.
while(i+1 < can.size() && can[i] == can[i+1])
i++;
// leave
backtrack(i+1, target, can);
}
};