JAVA 240ms recurse


  • 0
    L
    public List<List<Integer>> combinationSum3(int k, int n) {
        return comb(k,1,n);
    }
    public List<List<Integer>> comb(int k,int i,int n){
    	List<List<Integer>> res=new LinkedList<List<Integer>>();
    	if(k<0||n<0||i>9) return res;
    	if(k==1&&i==n){
    		LinkedList<Integer> cur=new LinkedList<Integer>();
    		cur.add(i);
    		res.add(cur);
    	}
    	for(List<Integer> t:comb(k,i+1,n)){
    		res.add(t);
    	}
    	for(List<Integer> t:comb(k-1,i+1,n-i)){
    		LinkedList<Integer> cur=new LinkedList<Integer>(t);
    		cur.addFirst(i);
    		res.add(cur);
    	}
    	return res;
    }

Log in to reply
 

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