My 212 ms Python Solution


  • 0
    C

    Hello, below is my Python solution. Feel free to leave any comments!
    '''
    class Solution(object):
    def combine(self, n, k):

        rlist = []
        if n < k:
            rlist = []
        elif n == k:
            rlist = [range(1, n+1)]
        elif k == 1: # base case
            rlist = [[i+1] for i in range(n)]
        elif k > 1:
            temp = self.combine(n-1, k-1) # scanning from small to big
            for s in temp:
                for num in range(s[-1]+1, n+1):
                    rlist.append(s+[num])
    
        return rlist
    

    '''


Log in to reply
 

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