Recursive Python Solutions beating 100%


  • 0
    1

    Here is my code that beats 100%:
    '''

    def combine(self, n, k):
        """
        :type n: int
        :type k: int
        :rtype: List[List[int]]
        """
        r = self.myCombine(0, n, 0, k)
        return r
    
    def myCombine(self, start, n, depth, k):
        if depth == k:
            return [[start]]
        r = []
        for i in range(start + 1, n  + 1 - (k - depth - 1)):
            t = self.myCombine(i, n, depth + 1, k)
            if start == 0:
               r += t
            else:
                for p in t:
                    p = [start] + p
                    r.append(p)
        return r
    

    '''
    hope it helps.


Log in to reply
 

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