-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCombinationsum.java
More file actions
26 lines (21 loc) · 878 Bytes
/
Combinationsum.java
File metadata and controls
26 lines (21 loc) · 878 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
Leetcode Combination Sum Problem Solution(Arrays)
class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> res = new ArrayList<>();
makeCombination(candidates, target, 0, new ArrayList<>(), 0, res);
return res;
}
private void makeCombination(int[] candidates, int target, int idx, List<Integer> comb, int total, List<List<Integer>> res) {
if (total == target) {
res.add(new ArrayList<>(comb));
return;
}
if (total > target || idx >= candidates.length) {
return;
}
comb.add(candidates[idx]);
makeCombination(candidates, target, idx, comb, total + candidates[idx], res);
comb.remove(comb.size() - 1);
makeCombination(candidates, target, idx + 1, comb, total, res);
}
}