forked from varunu28/LeetCode-Java-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCombination Sum.java
More file actions
22 lines (21 loc) · 816 Bytes
/
Combination Sum.java
File metadata and controls
22 lines (21 loc) · 816 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> result = new ArrayList<>();
helper(candidates, target, new ArrayList<>(), result, 0);
return result;
}
private void helper(int[] candidates, int target, List<Integer> combination, List<List<Integer>> result, int idx) {
if (target < 0 || idx == candidates.length) {
return;
}
if (target == 0) {
result.add(combination);
return;
}
for (int i = idx; i < candidates.length; i++) {
combination.add(candidates[i]);
helper(candidates, target - candidates[i], new ArrayList<>(combination), result, i);
combination.remove(combination.size() - 1);
}
}
}