39. Combination Sum

LeetCode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
private void util(int[] arr, int target, List<List<Integer>> res, List<Integer> ls,int start){
if(target<0){
return;
}else if(target==0){
res.add(new ArrayList(ls));
return;
}else{
for(int i=start;i<arr.length;i++){
ls.add(arr[i]);
util(arr,target-arr[i],res,ls,start);
ls.remove(ls.size()-1);
start+=1;
}
}
}
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> res=new ArrayList<>();
util(candidates,target,res,new ArrayList<>(),0);
return res;
}
}

0%