Leetcode: Permutations II

Question

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

For example,
[1,1,2] have the following unique permutations:
[1,1,2], [1,2,1], and [2,1,1].

Show Tags
Show Similar Problems

Solution

class Solution(object):
    def permuteUnique(self, nums):
        """ :type nums: List[int] :rtype: List[List[int]] """

        res = []
        if nums==[] or len(nums)==0:
            return res

        nums.sort()
        self.helper(nums, [], res)
        return res

    def helper(self, lst, item, res):
        if lst==[]:
            res.append(item)
            return

        for ind,elem in enumerate(lst):
            if ind>0 and lst[ind]==lst[ind-1]:
                continue
            temp = lst[:]
            del temp[ind]
            self.helper(temp, item+[elem], res)

你可能感兴趣的:(Leetcode: Permutations II)