Skip to content

Commit 17c86e8

Browse files
committed
comb sum solutions
1 parent 139e89d commit 17c86e8

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

combination-sum/dylan-jung.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public:
3+
vector<vector<int>> ans;
4+
vector<int> candids;
5+
int t;
6+
7+
void dfs(int idx, int sum, vector<int> s) {
8+
if(sum > t) return;
9+
else if (sum == t) ans.push_back(s);
10+
11+
for(int i = idx; i < candids.size(); i++) {
12+
auto next = vector<int>(s);
13+
next.push_back(candids[i]);
14+
dfs(i, sum+candids[i], next);
15+
}
16+
};
17+
18+
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
19+
candids = candidates;
20+
t = target;
21+
dfs(0, 0, {});
22+
return ans;
23+
}
24+
};

0 commit comments

Comments
 (0)