Easy to understand, verbose, average speed, Python recursive solution


  • 0
    G

    """

    class Solution(object):

    def combine(self, n, k):
        nums=list(range(1,n+1))
        return self.helper(nums,k)
    
    def helper(self, nums,r):
        if r==1:
            return [[num] for num in nums]
        if len(nums)==r:
            return [nums]
        output=[]
        for index in range(len(nums)-r+1):
            for remainder in self.helper(nums[index+1:],r-1):
                output.append([nums[index]]+remainder)
        return output

Log in to reply
 

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