One line recursive python without using itertools.permutations


  • 0
    A
    from itertools import chain, imap
        
        
    def permute(nums):
        return [nums] if len(nums) == 1 else list(chain.from_iterable(imap(
            lambda i: map(lambda sub: [nums[i]] + sub,
                          self.permute(nums[0:i] + nums[i + 1:])),
            range(len(nums))
        )))
    

    chain.from_iterable(imap(...)) is equivalent to flatMap.

    the 'lambda' function will produce a sublist those start with the same number, e.g. [[1, ...], [1, ...], [1, ...]]


Log in to reply
 

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