-
Notifications
You must be signed in to change notification settings - Fork 0
/
CombinationSumII.cpp
32 lines (30 loc) · 1.09 KB
/
CombinationSumII.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
#include <vector>
using namespace std;
class Solution {
public:
/* Sol 0
* Recursive solution.
*/
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
vector<vector<int>> result;
if(candidates.empty() or target == 0) return result;
sort(candidates.begin(),candidates.end(),[](int a,int b){return a>b;});//can also be done in ascending order
vector<int> temp;
combinationSum(candidates,target,0,result,temp);
return result;
}
void combinationSum(vector<int>& candidates,int target,int pos,vector<vector<int>> &result,vector<int> &temp){
if(target == 0){
result.push_back(temp);
return;
}
for(int i = pos;i < candidates.size();++i){
if(i != pos && candidates[i] == candidates[i-1]) continue;//skip the duplicated combinations
if(target-candidates[i] >= 0) {
temp.push_back(candidates[i]);
combinationSum(candidates,target-candidates[i],i+1,result,temp);
temp.erase(temp.end()-1);
}
}
}
};