-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path40.cpp
More file actions
executable file
·40 lines (40 loc) · 1.08 KB
/
40.cpp
File metadata and controls
executable file
·40 lines (40 loc) · 1.08 KB
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
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
class Solution {
public:
vector<int> tmp;
vector<int> gl_candidates;
int gl_target;
vector<vector<int> > answer;
void dfs(int index,int sum){
if (sum == gl_target){
vector<int> tmpanswer = tmp;
sort(tmpanswer.begin(),tmpanswer.end());
if (find(answer.begin(),answer.end(),tmpanswer) == answer.end())
answer.push_back(tmpanswer);
return;
}
if (index == gl_candidates.size())
return;
if (sum > gl_target)
return;
tmp.push_back(gl_candidates[index]);
dfs(index+1,sum+gl_candidates[index]);
tmp.pop_back();
dfs(index+1,sum);
}
vector<vector<int> > combinationSum2(vector<int>& candidates, int target) {
gl_candidates = candidates;
gl_target = target;
dfs(0,0);
return answer;
}
};
int main(){
Solution s;
int a[7] = {10,1,2,7,6,1,5};
vector<int> candidates(a,a+6);
s.combinationSum2(candidates,8);
}