用python实现不重复的全排列

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

#!/usr/bin/env python
# -*- coding:utf-8* --
# authour: Qing

class solution:
    def permute(self,num):
        if len(num)<=1:
            return [num];
        r = [];
        for i in range(len(num)):
            p = num[:i] + num[i+1:]; 
            x = self.permute(p);
            for ele in x:
                r.append(num[i:i+1]+ele);
        return r;
    
    def unione(self,num):
        unio = [];
        for i in range(len(num)):
            if num[i] not in unio:
                unio.append(num[i]);
        if len(unio) == 1:
            return num;
        else:
            return [[num[0],num[1]],[num[1],num[0]]];
        
    def unique(self,num):
        if len(num)<=2:
            return self.unione(num);
        unilist = [];
        allres = self.permuteUnique(num)
        for i in range(len(allres)):
            if allres[i] not in unilist:
                unilist.append(allres[i]);
        return unilist;

            

if __name__ == '__main__':
    l = [1,1,2]
    a = solution();
    print a.unique(l)