forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjinvicky.java
More file actions
32 lines (27 loc) · 1.02 KB
/
Copy pathjinvicky.java
File metadata and controls
32 lines (27 loc) · 1.02 KB
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
27
28
29
30
31
32
import java.util.ArrayList;
import java.util.List;
class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> answer = new ArrayList<>();
makeCombination(candidates, target, 0, new ArrayList<>(), 0, answer);
return answer;
}
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);
}
}