您的位置:首页 > 其它

Leetcode: Permutations II

2015-09-09 07:37 330 查看

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

[code]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)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: