Share My Easy Java Solution


  • 0
    A
    public class Solution {
    
    List<List<Integer>> re = new ArrayList<List<Integer>>();
    
    public List<List<Integer>> combinationSum3(int k, int n) {
    
    	helper(new ArrayList<Integer>(), 0, 1, k, n);
    	return re;
    }
    
    private void helper(List<Integer> curList, int curSum, int index, int k,
    		int n) {
    
    	if (curList.size() == k && curSum == n) {
    		re.add(new ArrayList<Integer>(curList));
    	}
    	if (curList.size() > k || curSum > n) {
    		return;
    	}
    	for(int i=index;i<=9;i++){
    		curSum += i;
    		curList.add(i);
    		helper(curList, curSum, i + 1, k, n);
    		curSum -= i;
    		curList.remove(curList.size() - 1);
    	}
    }
    

    }


Log in to reply
 

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