-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCombinationSum.java
More file actions
48 lines (38 loc) · 1.46 KB
/
CombinationSum.java
File metadata and controls
48 lines (38 loc) · 1.46 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
// https://leetcode.com/problems/combination-sum/
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class CombinationSum {
public static void main(String[] args) {
int[] nums = {2,3,6,7};
System.out.println( new CombinationSum().combinationSum(nums, 8));
int[] nums1 = {2,3,5};
System.out.println(new CombinationSum().combinationSum(nums1, 8));
}
public List<List<Integer>> combinationSum(int[] candidates, int target) {
Arrays.sort(candidates);
return combSum(candidates, target, 0);
}
public static List<List<Integer>> combSum(int[] sorted, int target, int index) {
ArrayList<List<Integer>> sol = new ArrayList<List<Integer>>();
if (target == 0) {
sol.add(new ArrayList<Integer>());
return sol;
}
if (target < 0 || index >= sorted.length)
return null;
int limit = target / sorted[index];
List<List<Integer>> reduced = new ArrayList<List<Integer>>();
for (int i = 0; i <= limit; i++) {
reduced = combSum(sorted, target - sorted[index] * i, index+1);
if (reduced != null) {
for (List<Integer> lst : reduced) {
for (int j = 1; j <= i; j++)
lst.add(sorted[index]);
}
sol.addAll(reduced);
}
}
return sol;
}
}