My accepted python solution


  • 0
    S
    class Solution(object):
    def permuteUnique(self, nums):
        """
        :type nums: List[int]
        :rtype: List[List[int]]
        """
        if len(nums) < 2:
            return [nums]
        
        nums.sort()
        solution = []
        results = []
        return self.mypermute(nums, solution, results)
    
    def mypermute(self, nums, solution, results):
        if len(nums) == 1:
            solution += nums
            results.append(solution) 
            return results
        
        temp = copy.copy(nums) 
        temp.remove(nums[0])
        results = self.mypermute(temp,solution+[nums[0]], results)
        
        for i in range(1,len(nums)):
            if nums[i] != nums[i-1]:
                temp = copy.copy(nums)
                temp.remove(nums[i])
                results = self.mypermute(temp, solution+[nums[i]], results)
        return results
    

    Compared to "Permutations" problem, the key idea here is to skip all repeated value in the array.


Log in to reply
 

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