-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution_39.java
More file actions
38 lines (35 loc) · 1.16 KB
/
Solution_39.java
File metadata and controls
38 lines (35 loc) · 1.16 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
package com.hilbert25.leetcode;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Solution_39 {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
Arrays.sort(candidates);
List<List<Integer>> result = new ArrayList<List<Integer>>();
List<Integer> current = new ArrayList<Integer>();
dfs(candidates, target, 0, 0, result, current);
return result;
}
public void dfs(int[] candidates, int target, int begin, int sum,
List<List<Integer>> result, List<Integer> current) {
int len = candidates.length;
for (int i = begin; i < len && sum + candidates[i] <= target; i++) {
if (sum + candidates[i] < target) {
sum += candidates[i];
current.add(candidates[i]);
dfs(candidates, target, i, sum, result, current);// 這裏錯了
sum = sum - current.get(current.size() - 1);
current.remove(current.size() - 1);
} else {
current.add(candidates[i]);
List<Integer> list = new ArrayList<Integer>(current.size());
list.addAll(current);
result.add(list);
sum = sum - current.get(current.size() - 1);
current.remove(current.size() - 1);
return;
}
}
return;
}
}