-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcombinationSum2.cpp
50 lines (43 loc) · 1.24 KB
/
combinationSum2.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
using std::cout;
using std::endl;
using std::vector;
using std::sort():
// void combination(vector<int>&, int, vector<vector<int> >, vector<int>&, int);
void combinate(vector<int>& candinates, int target, vector<vector<int> >& res, vector<int>& combination, int begin){
if(!target){
res.push_back(combination);
return;
}
for(int i = begin; i < candinates.size() ; i++){
combination.push_back(candinates[i]);
if(i && candinates[i] == candinates[i-1]) continue;
if(target - candinates[i] >= 0)
combinate(candinates, target - candinates[i], res, combination, i + 1);
combination.pop_back();
}
}
vector<vector<int> > combinationSum2(vector<int>& candinates, int target){
sort(candinates.begin(), candinates.end());
vector<vector<int> > res;
vector<int> combination;
combinate(candinates, target, res, combination, 0);
return res;
}
int main(){
vector<int> test = {10, 1, 2, 7, 6, 1, 5};
int target = 8;
vector<vector<int> > res = combinationSum(test, target);
for(int i = 0; i < res.size(); i++){
cout << endl;
vector<int> temp = res[i];
for(int j = 0; j < temp.size(); j++){
cout << temp[j] << "\t";
}
}
system("pause");
return 0;
}