My recursive Python solution


  • 0
    H

    Similar to commonly used backtracking recursive template. Using sort to handle duplication:

    def permuteUnique(self, nums):
        if not nums:
            return []
        nums = sorted(nums)
        res = []
        path = []        
        self.dfs(nums, path, res)
        return res
        
    def dfs(self, s, path, res):
        if not s:
            res.append(path)
            return
        
        for i in xrange(len(s)):
            if i>0 and s[i]==s[i-1]:
                continue
            self.dfs(s[:i]+s[i+1:], path+[s[i]], res)

Log in to reply
 

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