Beats 95% 16ms Java clean solution, easy understand


  • 0
    S
    List<List<Integer>> res;
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
    	res = new ArrayList<List<Integer>>();
    	helper(new ArrayList<Integer>(), candidates, 0, target);
    	return res;
    }
    
    private void helper(List<Integer> list, int[] candidates, int cur, int remain) {
    	if (remain == 0) {res.add(new ArrayList<Integer>(list)); return;}
    	else if (remain < 0 ) return;
    	for (int i = cur; i < candidates.length; i++) {
    		if (candidates[i] > remain) continue;
    		list.add(candidates[i]);
    		helper(list, candidates, i, remain-candidates[i]);
    		list.remove(list.size()-1);
    	}
    }
    

Log in to reply
 

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