Skip to content

Commit 262833a

Browse files
committed
combination sum solved
1 parent 0e116ed commit 262833a

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

combination-sum/sora0319.java

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

0 commit comments

Comments
 (0)