Accepted python solution share


  • 0
    L

    The key point is to keep combine from less to more.

    class Solution:
    # @return a list of lists of integers
    def combine(self, n, k):
        def combineCore(vals,k):
            if(k==1):
                vals.reverse();
                return [[i] for i in vals];
            else:
                res=[]
                for i in range(len(vals)-1,-1,-1):
                    newVals=vals[0:i];
                    prev_combine=combineCore(newVals,k-1);
    
                    for comb in prev_combine:
                        comb.append(vals[i]);
                        res.append(comb);
                return res;
    
        
        return combineCore([i for i in range(1,n+1)],k);

Log in to reply
 

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