-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode39.java
More file actions
53 lines (45 loc) · 1.68 KB
/
LeetCode39.java
File metadata and controls
53 lines (45 loc) · 1.68 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import util.PrintUtil;
import java.util.ArrayList;
import java.util.List;
public class LeetCode39 {
public static void main(String[] args) {
// 输入:candidates = [2,3,6,7], target = 7
// 输出:[[2,2,3],[7]]
PrintUtil.printNestedList(new Solution39().combinationSum(new int[] { 2, 3, 6, 7 }, 7));
// 输入: candidates = [2,3,5], target = 8
// 输出: [[2,2,2,2],[2,3,3],[3,5]]
PrintUtil.printNestedList(new Solution39().combinationSum(new int[] { 2, 3, 5
}, 8));
// 输入: candidates = [2], target = 1
// 输出: []
PrintUtil.printNestedList(new Solution39().combinationSum(new int[] { 2 },
1));
}
}
class Solution39 {
private List<List<Integer>> records = new ArrayList<>();
public List<List<Integer>> combinationSum(int[] candidates, int target) {
dfs(candidates, target, new ArrayList<>(), 0);
return records;
}
public void dfs(int[] candidates, int target, List<Integer> record, int candidateIndex) {
if (target == 0) {
records.add(new ArrayList<>(record));
return;
}
if (candidateIndex == candidates.length) {
return;
}
int candidate = candidates[candidateIndex];
for (int count = 0; count <= target / candidate; count++) {
// System.out.println(candidate + ": " + count);
for (int i = 0; i < count; i++) {
record.add(candidate);
}
dfs(candidates, target - count * candidate, record, candidateIndex + 1);
for (int i = 0; i < count; i++) {
record.removeLast();
}
}
}
}