-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path39.组合总和.java
37 lines (34 loc) · 1010 Bytes
/
39.组合总和.java
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
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/*
* @lc app=leetcode.cn id=39 lang=java
*
* [39] 组合总和
*/
// @lc code=start
class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> res = new ArrayList<>();
Arrays.sort(candidates);
List<Integer> path = new ArrayList<>();
method(candidates, target, path, res, 0);
return res;
}
public void method(int[] candidates, int target, List<Integer> path, List<List<Integer>> res, int startidx) {
if (target == 0) {
res.add(new ArrayList<>(path));
return;
}
if (target < 0) {
return;
}
int len = candidates.length;
for (int i = startidx; i < len; i++) {
path.add(candidates[i]);
method(candidates, target - candidates[i], path, res, i);
path.remove(path.size() - 1);
}
}
}
// @lc code=end