python写算法题:leetcode: 47. Permutations II

class Solution(object):
    def permuteUnique(self, nums):
        """
        :type nums: List[int]
        :rtype: List[List[int]]
        """
        ret=[nums,]
        redup=set()
        redup.add(tuple(nums))
        for i in xrange(len(nums)-1):
            for r in ret[:]:
                for j in xrange(i+1,len(nums)):
                    newr=r[:]
                    newr[i],newr[j]=newr[j],newr[i]
                    if tuple(newr) not in redup:
                        redup.add(tuple(newr))
                        ret.append(newr)
        return ret

 

点赞