-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCombinationSum.java
More file actions
27 lines (23 loc) · 815 Bytes
/
CombinationSum.java
File metadata and controls
27 lines (23 loc) · 815 Bytes
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
import java.util.List;
import java.util.ArrayList;
public class CombinationSum {
public void findCombinations(int index,int[] arr, int target,List<List<Integer>> ans, List<Integer> ds){
if(index == arr.length){
if(target == 0){
ans.add(new ArrayList<>(ds));
}
return;
}
if(arr[index] <= target){
ds.add(arr[index]);
findCombinations(index,arr,target - arr[index],ans,ds);
ds.remove(ds.size() -1);
}
findCombinations(index +1,arr,target,ans,ds);
}
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> ans = new ArrayList<>();
findCombinations(0,candidates,target,ans,new ArrayList<>());
return ans;
}
}