-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathCombination Sum (by Raymond)
More file actions
34 lines (34 loc) · 1.03 KB
/
Combination Sum (by Raymond)
File metadata and controls
34 lines (34 loc) · 1.03 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
public class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
Arrays.sort(candidates);
List<List<Integer>> res = new ArrayList<List<Integer>>();
if(candidates.length>0&&target==candidates[0]){
res.add(new ArrayList<Integer>());
res.get(0).add(target);
return res;
}
if(candidates.length==0||target<candidates[0]){
return res;
}
List<List<Integer>> res1 = combinationSum(candidates,target-candidates[0]);
int[] arr = new int[candidates.length-1];
int i = 0;
while(i<arr.length){
arr[i]=candidates[i+1];
i++;
}
List<List<Integer>> res2 = combinationSum(arr,target);
i = 0;
while(i<res1.size()){
res1.get(i).add(0,candidates[0]);
res.add(res1.get(i));
i++;
}
i=0;
while(i<res2.size()){
res.add(res2.get(i));
i++;
}
return res;
}
}