Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 89f33c1

Browse files
authored
Create CombinationSum.java
1 parent c5d2cae commit 89f33c1

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

‎Backtracking/CombinationSum.java‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//LeetCode 39. Combination Sum
2+
//Question - https://leetcode.com/problems/combination-sum/
3+
4+
class Solution {
5+
public List<List<Integer>> combinationSum(int[] nums, int target) {
6+
List<List<Integer>> res = new ArrayList<>();
7+
helper(nums, 0, 0, target, new ArrayList<>(), res);
8+
return res;
9+
}
10+
11+
public void helper(int nums[], int index, int sum, int target, List<Integer> l, List<List<Integer>> res){
12+
if(sum > target) return;
13+
else if(sum == target){
14+
res.add(new ArrayList<>(l));
15+
return;
16+
}
17+
18+
for(int i = index ; i < nums.length ; i++){
19+
20+
l.add(nums[i]);
21+
helper(nums, i, sum + nums[i], target, l, res);
22+
l.remove(l.size()-1);
23+
}
24+
25+
}
26+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /