|
| 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