2 Line Recursive Python Solution


  • 0
    S

    Using recursion, list comprehension, lambda expressions, and the built in reduce and map functions, I created this solution.

    It isn't the most elegant, but it beats 70% of other solutions, and I thought it was cool having it be only 2 lines :D

    def permute(self, nums):
        if len(nums) == 1: return [nums]
        return reduce(lambda y,z:y+z, [map(lambda x:[num1]+x, self.permute([num2 for i,num2 in enumerate(nums) if i!=j])) for j,num1 in enumerate(nums)])
    

Log in to reply
 

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