My one cent in Java


  • 0
    K
    public class Solution {
            public List<List<Integer>> combinationSum(int[] candidates, int target) {
    	        List<List<Integer>> lists = new ArrayList<>();
    	        if(candidates.length == 0) return lists;
    	        List<Integer> list = new ArrayList<>();
    	        combinationSum(candidates, target, 0, list, lists);
    	        return lists;
    	    }
    	    
    	    private void combinationSum(int[] c, int target, int j, List<Integer> l, List<List<Integer>> ls) {
    	        for(int i=j; i < c.length; i++) {
    	            l.add(c[i]);
    	            target -= c[i];
    	            if(target < 0) { 
    	            	l.remove(l.size()-1); 
    	            	target += c[i]; 
    	            	continue; 
    	            }
    	            combinationSum(c, target, i, l, ls);
    	            if(0 == target) { ls.add(new ArrayList<>(l)); }
    	            l.remove(l.size()-1);
    	            target += c[i];
    	        }
    	    }
    }
    

Log in to reply
 

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