-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCombinationSum.java
More file actions
58 lines (51 loc) · 1.77 KB
/
CombinationSum.java
File metadata and controls
58 lines (51 loc) · 1.77 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
54
55
56
57
58
package com.leetcode.backtracking;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* @author: yhl
* @DateTime: 2021/2/26 17:26
* @Description: 组合总和
*/
public class CombinationSum {
public static void main(String[] args) {
//candidates = [2,3,6,7], target = 7
int[] candidates = {2,3,6,7};
CombinationSum combinationSum = new CombinationSum();
List<List<Integer>> result = combinationSum.combinationSum(candidates, 7);
System.out.println(result);
}
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> res = new ArrayList<>();
if (candidates.length == 0) {
return res;
}
// 排序是重点,如果加上当前元素,超过target,那么就不用考虑下一位元素了,直接撤消两位
Arrays.sort(candidates);
List<Integer> path = new ArrayList<>();
dfs(candidates, 0, target, 0, path, res);
return res;
}
private void dfs(int[] candidates, int begin, int target, int sum, List<Integer> path,
List<List<Integer>> res) {
if (sum == target) {
res.add(new ArrayList<>(path));
return;
}
for (int i = begin; i < candidates.length; i++) {
// 必须结束循环
if (candidates[i] > target - sum) {
return;
}
if (i > 0 && candidates[i-1] == candidates[i]) {
continue;
}
path.add(candidates[i]);
sum += candidates[i];
dfs(candidates, i, target, sum, path, res);
// 重点 撤消操作
sum -= candidates[i];
path.remove(path.size() - 1);
}
}
}