-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCombinationSum.java
More file actions
31 lines (29 loc) · 1.04 KB
/
CombinationSum.java
File metadata and controls
31 lines (29 loc) · 1.04 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
import java.util.*;
public class CombinationSum {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> result = new ArrayList<>();
Arrays.sort(candidates);
backtrack(candidates,target,0,new ArrayList<>(),result);
return result;
}
private void backtrack(int[] candidates, int target, int start, List<Integer> current, List<List<Integer>> result){
if(target==0){
result.add(new ArrayList<>(current));
return;
}
if(target<0){
return;
}
for(int i=start; i<candidates.length; i++){
current.add(candidates[i]);
backtrack(candidates, target-candidates[i], i, current, result);
current.remove(current.size()-1);
}
}
public static void main(String[] args) {
CombinationSum cs = new CombinationSum();
int[] candidates = {2,3,6,7};
int target = 7;
System.out.println(cs.combinationSum(candidates,target));
}
}