Concise java solution 1ms


  • 0
    C
    public class Solution {
    List<List<Integer>> result;
    public List<List<Integer>> combinationSum3(int k, int n) {
        result=new ArrayList();
        List<Integer>solution=new ArrayList();
        find(k,n,1,solution);
        return result;
    }
    
    public void find(int k,int n,int start,List<Integer>solution){
        
        if(k==1){
            List<Integer>current=new ArrayList(solution);
            if(n>=start && n<=9){
                current.add(n);
                result.add(current);
                return;
            }else{
                return;
            }
        }
        for(int i=start;i<=10-k;i++){
            List<Integer>current=new ArrayList(solution);
            current.add(i);
            find(k-1,n-i,i+1,current);
        }
    }
    

    }


Log in to reply
 

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