Skip to content

Commit 70fba59

Browse files
committed
Runtime: 101 ms (Top 24.96%) | Memory: 22 MB (Top 28.36%)
1 parent 659e51f commit 70fba59

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
1+
// Runtime: 101 ms (Top 24.96%) | Memory: 22 MB (Top 28.36%)
12
class Solution {
23
void combination(vector<int>& candidates, int target, vector<int> currComb, int currSum, int currIndex, vector<vector<int>>& ans){
34
if(currSum>target) return; //backtrack
45
if(currSum==target){
56
ans.push_back(currComb); //store the solution and backtrack
67
return;
78
}
8-
9+
910
for(int i=currIndex; i<candidates.size(); i++){ //try all possible options for the next level
1011
currComb.push_back(candidates[i]); //put 1 option into the combination
1112
currSum+=candidates[i];
1213
combination(candidates, target, currComb, currSum, i, ans); //try with this combination, whether it gives a solution or not.
1314
currComb.pop_back(); //when this option backtrack to here, remove this and go on to the next option.
1415
currSum-=candidates[i];
1516
}
16-
17+
1718
}
1819
public:
1920
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
@@ -23,4 +24,3 @@ class Solution {
2324
return ans;
2425
}
2526
};
26-

0 commit comments

Comments
 (0)