-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCombinationSum.java
More file actions
31 lines (26 loc) · 940 Bytes
/
CombinationSum.java
File metadata and controls
31 lines (26 loc) · 940 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
28
29
30
31
package Recursion;
import java.util.ArrayList;
import java.util.List;
/*
39. Combination Sum
*/
public class CombinationSum {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> res = new ArrayList<>();
dfs(candidates,target,new ArrayList<>(),res,0,0);
return res;
}
public void dfs(int[] canidates, int target, List<Integer> curr,List<List<Integer>> res, int index, int total){
if(total==target){
res.add(new ArrayList<>(curr));
return;
}
if(index >= canidates.length || total > target) return;
// Choose to include current integer
curr.add(canidates[index]);
dfs(canidates,target,curr,res,index,total+canidates[index]);
//Choose to not include current integer
curr.remove(curr.get(curr.size() - 1));
dfs(canidates,target,curr,res,index+1,total);
}
}