Skip to content

Commit 9d49851

Browse files
committed
add solution of combination-sum
1 parent 8a1c8c4 commit 9d49851

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

combination-sum/jinhyungrhee.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import java.util.*;
2+
class Solution {
3+
public List<List<Integer>> output;
4+
public List<List<Integer>> combinationSum(int[] candidates, int target) {
5+
output = new ArrayList<>();
6+
Deque<Integer> stack = new ArrayDeque<>();
7+
dfs(candidates, target, stack, 0, 0);
8+
return output;
9+
10+
}
11+
12+
public void dfs(int[] candidates, int target, Deque<Integer> stack, int start, int total) {
13+
14+
if (total > target) return;
15+
16+
if (total == target) {
17+
output.add(new ArrayList<>(stack));
18+
return;
19+
}
20+
21+
for (int i = start; i < candidates.length; i++) {
22+
int num = candidates[i];
23+
stack.push(num);
24+
dfs(candidates, target, stack, i, total + num);
25+
stack.pop();
26+
}
27+
}
28+
}

0 commit comments

Comments
 (0)