-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCombination Sum.cpp
More file actions
33 lines (28 loc) · 853 Bytes
/
Combination Sum.cpp
File metadata and controls
33 lines (28 loc) · 853 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
30
31
32
33
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
vector<vector<int>> combinationSum(vector<int>& candidates, int target)
{
vector<vector<int>> res;
sort(candidates.begin(), candidates.end());
for(int i = 0; i < candidates.size(); i++)
{
if(candidates[i] > target) break;
if(candidates[i] == target)
{
res.push_back({candidates[i]});
break;
}
vector<int> vec = vector<int>(candidates.begin() + i, candidates.end());
vector<vector<int>> tmp = combinationSum(vec, target - candidates[i]);
for(auto a: tmp)
{
a.insert(a.begin(), candidates[i]);
res.push_back(a);
}
}
return res;
}
};