With list comprehension, you don't really need to get that complicated with your code


  • 2
    B
    class Solution:
        # @param num, a list of integer
        # @return a list of lists of integers
        def permute(self, num):
            '''
            Don't consider duplicates.
            If len(num) < 1, return [[]].
            Insert each new value into all possible interspaces, 
                which there are n+1 of them if there have been n characters already.
            '''
            results = [[]]
            for x in num:
                results = [r[:i] + [x] + r[i:] for r in results for i in range(len(r)+1)]
            return results

Log in to reply
 

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