Share Java solution(6ms) using recursive


  • 0
    H

    public class Solution {

    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        List<List<Integer>> res = new LinkedList<List<Integer>>();
        Arrays.sort(candidates);
        combinationSum(candidates, target, null, res, 0);
        return res;
    }
    public void helper(int[] candidates, int target, LinkedList<Integer> l, List<List<Integer>> res, int start){
        if(l==null){
            l = new LinkedList<Integer>();
        }
        for(int i=start;i<candidates.length;i++){
            if(candidates[i]==target){
                l.add(candidates[i]);
                res.add(l);
                return;
            }
            if(candidates[i]>target){
                return;
            }
            LinkedList<Integer> l2 = (LinkedList)l.clone();
            l2.add(candidates[i]);
            helper(candidates, target-candidates[i], l2, res, i);
        }
        
    }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.