-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomb.java
More file actions
25 lines (25 loc) · 792 Bytes
/
Copy pathcomb.java
File metadata and controls
25 lines (25 loc) · 792 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
class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> ans=new ArrayList<>();
findans(0,ans,candidates,target,new ArrayList<>());
return ans;
}
public static void findans(int ind,List<List<Integer>> ans, int [] candidates,int target,List<Integer> addd)
{
if(candidates.length==ind)
{
if(target==0)
{
ans.add(new ArrayList<>(addd));
}
return ;
}
if(candidates[ind]<=target)
{
addd.add(candidates[ind]);
findans(ind,ans,candidates,target-candidates[ind],addd);
addd.remove(addd.size()-1);
}
findans(ind+1,ans,candidates,target,addd);
}
}