-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
combinationSum.cpp
28 lines (24 loc) · 896 Bytes
/
combinationSum.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 {
private:
void findCombinations(vector<int>& candidates, int index, int target, vector<int> current, vector<vector<int>>& result) {
if(target < 0) return;
if(target == 0) {
result.push_back(current);
return;
}
for(int i = index; i < candidates.size(); ++i) {
if(candidates[i] <= target) {
current.push_back(candidates[i]);
findCombinations(candidates, i, target - candidates[i], current, result);
current.pop_back();
}
}
}
public:
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
vector<vector<int>> result;
sort(candidates.begin(), candidates.end());
findCombinations(candidates, 0, target, vector<int>(), result);
return result;
}
};