Skip to content

Commit 9231eb9

Browse files
authored
Merge pull request #97 from ayushijain22/patch-3
Added solution to problem.39 combination Sum
2 parents fbe87a8 + e97d3d9 commit 9231eb9

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

leetcode/CombinationSum

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class CombinationSum {
2+
public List<List<Integer>> combinationSum(int[] candidates, int target) {
3+
List<List<Integer>>res=new ArrayList();
4+
backtrack(candidates,0,target,new ArrayList(),res);{
5+
return res;
6+
}}
7+
private void backtrack(int[]cand,int start,int target,List<Integer>list,List<List<Integer>>result){
8+
if(target<0)
9+
return;
10+
else if(target==0)
11+
result.add(new ArrayList<>(list));
12+
for(int i=start;i<cand.length;i++)
13+
{
14+
15+
list.add(cand[i]);
16+
backtrack(cand,i,target-cand[i],list,result);
17+
list.remove(list.size()-1);
18+
}}
19+
}

leetcode/java/CombinationSum.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class CombinationSum {
2+
public List<List<Integer>> combinationSum(int[] candidates, int target) {
3+
List<List<Integer>>res=new ArrayList();
4+
backtrack(candidates,0,target,new ArrayList(),res);{
5+
return res;
6+
}}
7+
private void backtrack(int[]cand,int start,int target,List<Integer>list,List<List<Integer>>result){
8+
if(target<0)
9+
return;
10+
else if(target==0)
11+
result.add(new ArrayList<>(list));
12+
for(int i=start;i<cand.length;i++)
13+
{
14+
15+
list.add(cand[i]);
16+
backtrack(cand,i,target-cand[i],list,result);
17+
list.remove(list.size()-1);
18+
}}
19+
}

0 commit comments

Comments
 (0)