-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
37 lines (32 loc) · 1.07 KB
/
Solution.java
File metadata and controls
37 lines (32 loc) · 1.07 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
package leetCode_39;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* @author dimdark
*/
public class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> results = new ArrayList<List<Integer>>();
if (candidates == null || candidates.length == 0 || target <= 0) {
return results;
}
// Arrays.sort(candidates);
backtrack(results, new ArrayList<Integer>(), candidates, target, 0);
return results;
}
private void backtrack(List<List<Integer>> results,
List<Integer> list, int[] candidates, int remain, int start) {
if (remain <= 0) {
if (remain == 0) {
results.add(new ArrayList<Integer>(list));
}
return;
}
for(int i = start; i < candidates.length; ++i) {
list.add(candidates[i]);
backtrack(results, list, candidates, remain - candidates[i], i);
list.remove(list.size() - 1);
}
}
}