-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCombinationSum.java
More file actions
28 lines (27 loc) · 925 Bytes
/
Copy pathCombinationSum.java
File metadata and controls
28 lines (27 loc) · 925 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
import java.util.ArrayList;
import java.util.List;
public class CombinationSum {
public static void main(String[] args) {
int[] candidates={2,3,6,7};
int target=7;
System.out.println(combinationSum(candidates, target));
}
public static List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> res=new ArrayList<>();
solve(candidates,target,0,new ArrayList<>(),res);
return res;
}
public static void solve(int[] candidates,int target,int i,List<Integer> ls,List<List<Integer>> res){
if(target==0){
res.add(new ArrayList<>(ls));
return;
}
if(target<0 || i==candidates.length){
return;
}
ls.add(candidates[i]);
solve(candidates,target-candidates[i],i,ls,res);
ls.remove(ls.size()-1);
solve(candidates,target,i+1,ls,res);
}
}