Super easy-to-understand Python recursive with one line condition to trim dup. No visited[]


  • 0
    L
    class Solution(object):
        def permuteUnique(self, nums):
            """
            :type nums: List[int]
            :rtype: List[List[int]]
            """
            result = []
            self.perm(sorted(nums), [], result)
            return result
    
        def perm(self, input, path, paths):
            if len(input) == 0:
                paths.append(path[::])
                return
            for i in range(len(input)):
                if i > 0 and input[i] == input[i - 1]:
                    continue
                path.append(input[i])
                self.perm(input[:i] + input[i + 1:], path, paths)
                path.pop()
            return

Log in to reply
 

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