02day_leetcode_ Permutations

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].

 

本人思路代码(超时):

class Solution:
    # @param num, a list of integer
    # @return a list of lists of integers
    def permuteUnique(self, num):
        # 如蚕判断
        #import copy
        #
        perlist = [[]]
        for i in num:
            #print("i = %s" % i)
            for index in range(len(perlist)):

                perlist[index].append(i) 
                for j in range(len(perlist[index])):
                    new_item = perlist[index][:-1]
                    
                    new_item.insert(j, i)
                    perlist.append(new_item)

         
        perlist = [tuple(value) for value in perlist ]
        
        perlist = [list(value) for value in set(perlist)]
        print (perlist) 

 

posted @ 2014-12-09 23:23  xiaolongxia  阅读(77)  评论(0)    收藏  举报