An accepted Java solution


  • 2
    O
    public class Solution {
    public List<List<Integer>> combinationSum3(int k, int n) {
        List<Integer> item = new ArrayList<Integer>();
        List<List<Integer>> res = new ArrayList<List<Integer>>();
        helper(k,n,1,item,res);
        return res;
    }
    private void helper(int k, int target, int start, List<Integer> item, List<List<Integer>> res){
        if(k==0 && target==0){
            res.add(new ArrayList(item));
            return;
        }
        if(k==0 || target<=0){
            return;
        }
        for(int i=start; i<=9; i++){
            item.add(i);
            helper(k-1, target-i, i+1, item, res);
            item.remove(item.size()-1);
        }
    }
    

    }


Log in to reply
 

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