Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time

Given a collection of numbers that might contain duplicates, return all possible unique permutations.

Example:

Input: [1,1,2] Output: [ [1,1,2], [1,2,1], [2,1,1] ]

My code:

class Solution:
    def permuteUnique(self, nums):
        """
        :type nums: List[int]
        :rtype: List[List[int]]
        """
        all_lists=self.sub_permute(nums)
        return all_lists
    def sub_permute(self,nums):
        if len(nums)==1:
            return [[nums[0]]]
        lists=[]
        for item in nums:
            tmp_nums=nums.copy()
            tmp_nums.remove(item)
            tmp_list=self.sub_permute(tmp_nums)
            for item1 in tmp_list:
                tmp_use_list=[]
                tmp_use_list.append(item)
                tmp_use_list=tmp_use_list+item1
                if tmp_use_list not in lists:
                    lists.append(tmp_use_list)
        return lists
        

Solution: https://leetcode.com/problems/permutations-ii/