-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathLeetCode#32.cc
47 lines (45 loc) · 1.55 KB
/
LeetCode#32.cc
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
class Solution {
private:
void dfs(int ind , vector<int>& cand, int target, int sum ,vector<int>& col,
vector<vector<int> >& res){
if(sum==target){
vector<int> tmp;
for(int i=0;i<cand.size();i++)
if(col[i]){
for(int j=0;j<col[i];j++)
tmp.push_back(cand[i]);
}
res.push_back(tmp);
return ;
}
if(sum>target) return ;
if(ind==cand.size()) return ;
dfs(ind+1,cand,target,sum,col,res);
while(sum<=target){
col[ind]++;
sum+=cand[ind];
dfs(ind+1,cand,target,sum,col,res);
}
col[ind]=0;
return ;
}
public:
vector<vector<int> > combinationSum(vector<int> &candidates, int target) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<vector<int> > ret;
sort(candidates.begin(),candidates.end());
int size = candidates.size();
if(size==0) return ret;
vector<int> cand;
cand.push_back(candidates[0]);
for(int i=1;i<size;i++)
if(candidates[i]!=candidates[i-1])
cand.push_back(candidates[i]);
size = cand.size();
vector<int> col(size);
for(int i=0;i<size;i++) col[i]=0;
dfs(0,cand,target,0,col,ret);
return ret;
}
};