5 Lines Map/Reduce


  • 3
    K
    def permute(self, num):
        def generate(i):
            num[0], num[i] = num[i], num[0]
            return [[num[0]] + each for each in self.permute(num[1:])]
        return reduce(lambda x, y: x + y, map(generate, range(len(num))), []) or [[]]
    

    The key idea is to enumerate the first value and recursive on the rest of the list, which has similar structure with Generate parentheses.

    Map/reduce is abstract and expressive.


Log in to reply
 

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