Clean Java solution 1ms


  • 0
    S
    List<List<Integer>> res;
    public List<List<Integer>> combinationSum3(int k, int n) {
    	res = new ArrayList<List<Integer>>();
    	List<Integer> list  = new ArrayList<Integer>();
    	helper(list, 1, k, n);
    	return res;
    }
    
    private void helper(List<Integer> list, int i, int k, int n) {
    	if (k==1 && n<10) {
    		list.add(n);
    		res.add(list);
    		return;
    	}
    	while (n-i>i && i<10){
    		List<Integer> newList = new ArrayList<Integer>(list);
    		newList.add(i);
    		helper(newList, i+1, k-1, n-i);
    		i++;
    	}
    }
    

Log in to reply
 

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