-
Notifications
You must be signed in to change notification settings - Fork 0
/
Combination_Sum.java
33 lines (31 loc) · 1.04 KB
/
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
25
26
27
28
29
30
31
32
33
package com.leet_code;
import java.util.ArrayList;
import java.util.List;
public class Combination_Sum {
public static void main(String[] args) {
int[] a={2,3,5};
int t=8;
for (List<Integer> i : combinationSum(a,t)) {
System.out.print(i);
}
}
public static List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> res=new ArrayList<>();
List<Integer>ds=new ArrayList<>();
recursion(0,candidates,target,ds,res);
return res;
}
private static void recursion(int start,int[] candidates,int target,List<Integer>list,List<List<Integer>>res){
if (start== candidates.length){
if (target == 0) {
res.add(new ArrayList<>(list));
}
return;
}if(candidates[start]<=target) {
list.add(candidates[start]);
recursion(start,candidates,target-candidates[start],list,res);
list.removeLast();
}
recursion(start+1,candidates,target,list,res);
}
}