-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCombination Sum.cpp
More file actions
29 lines (29 loc) · 881 Bytes
/
Combination Sum.cpp
File metadata and controls
29 lines (29 loc) · 881 Bytes
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
class Solution {
public:
void DFS(int id, int target, vector<int> num, vector<int> &result, vector<vector<int> > &ans)
{
if(target==0)
{
ans.push_back(result);
return ;
}
for(int i=id;i<num.size();i++)
{
if(target-num[i]>=0)
{
result.push_back(num[i]);
DFS(i, target-num[i], num, result, ans);
result.pop_back();
}
}
}
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> > ans;
vector<int> result;
sort(candidates.begin(), candidates.end());
DFS(0, target, candidates, result, ans);
return ans;
}
};