Skip to content

Commit 0939e59

Browse files
authored
Merge pull request #695 from nilesh0100/nilesh0100-patch-2
Added Solution for LeetCode Problem No. 39
2 parents 255bd6b + 2c27e09 commit 0939e59

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// https://leetcode.com/problems/combination-sum/
2+
3+
class Solution {
4+
public List<List<Integer>> combinationSum(int[] nums, int target) {
5+
List<List<Integer>> ans = new ArrayList<>();
6+
findSum(nums,target,0,new ArrayList<Integer>(),ans);
7+
return ans;
8+
}
9+
10+
void findSum(int[] nums,int target, int ind,ArrayList<Integer> al, List<List<Integer>> ans)
11+
{
12+
if(ind==nums.length){
13+
if(target==0)
14+
{
15+
ans.add(new ArrayList<>(al));
16+
return;
17+
}
18+
return;
19+
}
20+
21+
if(nums[ind]<=target)
22+
{
23+
al.add(nums[ind]);
24+
findSum(nums,target-nums[ind],ind,al,ans);
25+
al.remove(al.size()-1);
26+
}
27+
findSum(nums,target,ind+1,al,ans);
28+
}
29+
}

0 commit comments

Comments
 (0)