-
Notifications
You must be signed in to change notification settings - Fork 0
/
Combination Sum.java
24 lines (24 loc) · 1009 Bytes
/
Combination Sum.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
class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> result = new ArrayList<List<Integer>>();
if(candidates == null || candidates.length == 0) return result;
ArrayList<Integer> current = new ArrayList<Integer>();
Arrays.sort(candidates);
combinationSum(candidates, target, 0, current, result);
return result;
}
public void combinationSum(int[] candidates, int target, int j, ArrayList<Integer> curr, List<List<Integer>> result){
if(target == 0){
ArrayList<Integer> temp = new ArrayList<Integer>(curr);
result.add(temp);
return;
}
for(int i=j; i<candidates.length; i++){
if(target < candidates[i])
return;
curr.add(candidates[i]);
combinationSum(candidates, target - candidates[i], i, curr, result);
curr.remove(curr.size()-1);
}
}
}