My Solution beat 100%


  • 0
    L
    public class Solution {
        public List<List<Integer>> combinationSum(int[] candidates, int target) {
            Arrays.sort(candidates);
            return combination(candidates, target, 0);
        }
        
        public List<List<Integer>> combination(int[] candidates, int target, int start) {
            List<List<Integer>> list = new ArrayList<>();
            if(candidates == null || candidates.length == 0) return list;
            
            for(int i = start; i < candidates.length; i++){
                if(candidates[i] < target){
                    List<List<Integer>> tlist = combination(candidates, target - candidates[i], i);
                    if(tlist.size() > 0){
                        for(List<Integer> alist : tlist){
                            alist.add(0, candidates[i]);
                        }
                        list.addAll(tlist);
                    }
                }
                else if(candidates[i] == target){
                    List<Integer> tlist = new LinkedList<>();
                    tlist.add(target);
                    list.add(tlist);
                }
                else ;
            }
            return list;
        }
    }
    

Log in to reply
 

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