-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathSolution39.java
More file actions
executable file
·35 lines (30 loc) · 1.44 KB
/
Solution39.java
File metadata and controls
executable file
·35 lines (30 loc) · 1.44 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
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Solution39 {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> result = new ArrayList<>();
ArrayList<Integer> tmp_list = new ArrayList<>();
Arrays.sort(candidates);
help(candidates, target, 0, result, tmp_list);
return result;
}
public void help(int[] candidates, int target, int start, List<List<Integer>> result, ArrayList<Integer> tmp_list) {
// target小于0,匹配失败
if (target < 0) return;
// target==0,及目标值
if (target == 0) {
//result 是List<List<Integer>>的,而tmp_list是list的,需要把tmp_list再裹一层new ArrayList<>(tmp_list)
result.add(new ArrayList<>(tmp_list));
return;
}
for (int position = start; position < candidates.length; position++) {
// 如果最小值大于剩余额度,则无解跳出即可
if (target < candidates[position]) break;
tmp_list.add(candidates[position]);
// 需要注意,这边位置应该是position,而不是start,position是为了避免每次都从头开始找,实际上,只需要考虑当前位置之和的值即可
help(candidates, target - candidates[position], position, result, tmp_list);
tmp_list.remove(tmp_list.size() - 1);
}
}
}