Easy Python recursive solution 64ms


  • 2
    M
    def combinations(n, k, start=1):
        if k == 1:
            return [[x,] for x in xrange(start, n+1)]
     
        Result = []
        for FirstNum in xrange(start, n - k + 2):
            for Comb in combinations(n, k-1, FirstNum + 1):
                Result.append([FirstNum,] + Comb)
        return Result

Log in to reply
 

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