Skip to content

Commit 6e78513

Browse files
committed
Add Solution.java to problems 0039
1 parent b161c02 commit 6e78513

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public List<List<Integer>> combinationSum(int[] candidates, int target) {
3+
List<List<Integer>> res = new ArrayList<>();
4+
combinationSum(candidates, target, 0, new Stack<>(), res);
5+
return res;
6+
}
7+
8+
private void combinationSum(int[] candidates, int target, int index, Stack<Integer> stack, List<List<Integer>> res) {
9+
if (target < 0) {
10+
return;
11+
}
12+
13+
if (target == 0) {
14+
res.add(new ArrayList<>(stack));
15+
return;
16+
}
17+
18+
for (int i = index; i < candidates.length; i++) {
19+
stack.push(candidates[i]);
20+
combinationSum(candidates, target - candidates[i], i, stack, res);
21+
stack.pop();
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)