forked from larissalages/code_problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path39.cpp
More file actions
23 lines (22 loc) · 779 Bytes
/
39.cpp
File metadata and controls
23 lines (22 loc) · 779 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
public:
void helper(vector<int>& candidates,vector<vector<int>>& ans,vector<int>& combination,int target,int start){
if(target==0){
ans.push_back(combination);
return;
}
for(int i=start;i<candidates.size();i++){
if(candidates[i]>target) break;
combination.push_back(candidates[i]);
helper(candidates,ans,combination,target-candidates[i],i);
combination.pop_back();
}
}
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
vector<vector<int>> ans;
vector<int> combination;
sort(candidates.begin(),candidates.end());
helper(candidates,ans,combination,target,0);
return ans;
}
};