-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCombination Sum
More file actions
24 lines (23 loc) · 773 Bytes
/
Copy pathCombination Sum
File metadata and controls
24 lines (23 loc) · 773 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
class Solution {
public:
void find(vector<int> &candidates, int target, vector<vector<int>> &out, vector<int> &in, int strt)
{
int len = candidates.size();
if(target == 0) {out.push_back(in);return;}
for(; strt < len; strt++)
{
if(candidates[strt] > target) continue;
in.push_back(candidates[strt]);
int n = target - candidates[strt];
find(candidates, n, out, in, strt);
in.pop_back();
}
}
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
vector<vector<int>> out;
vector<int> in;
sort(candidates.begin(), candidates.end());
find(candidates, target, out, in, 0);
return out;
}
};