-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathCombination_Sum.cpp
28 lines (26 loc) · 958 Bytes
/
Combination_Sum.cpp
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
class Solution {
public:
void combinationSumUtils(int indx, int sum, vector<int> &candidates, vector<int> &solution, vector<vector<int> > &result) {
if(sum == 0) {
result.push_back(solution);
return;
}
if(sum < 0) {
return;
}
for(int i = indx; i < candidates.size(); ++i) {
if(candidates[i] > sum) { break; }
solution.push_back(candidates[i]);
combinationSumUtils(i, sum - candidates[i], candidates, solution, result);
solution.pop_back();
}
}
vector<vector<int> > combinationSum(vector<int> &candidates, int target) {
vector<vector<int> > result;
vector<int> solution;
if(candidates.empty()) return result;
sort(candidates.begin(), candidates.end());
combinationSumUtils(0, target, candidates, solution, result);
return result;
}
};