码迷,mamicode.com
首页 > 编程语言 > 详细

Permutations【python】

时间:2014-08-27 20:11:28      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   for   ar   div   log   

class Solution:
    # @param num, a list of integer
    # @return a list of lists of integers
    def permute(self, num):
        length=len(num)
        if length==1:
            return [num]
        else:
            res=[]
            for i in range(length):
                d=self.permute(num[0:i]+num[i+1:length])
                for j in d:
                    res.append(j+[num[i]])
            #去除重复项
            k=len(res)-1
            while k>0:
                for j in range(k):
                    if self.IsSameList(res[j], res[k]):
                        del res[k]
                        break
                k-=1   
            return res
        
    def IsSameList(self,A,B):
        an=len(A)
        bn=len(B)
        if (an!=bn):
            return False
        else:
            for i in range(an):
                if (A[i]!=B[i]):
                    return False
            return True
         
if __name__==__main__:
    num=[1,2,3]
    s=Solution()
    print(s.permute([1,1,3]))

 C++的版本:http://hi.baidu.com/li_iois/item/4e093bda9b9bc22039f6f7aa(以前写的)

Permutations【python】

标签:style   blog   http   color   io   for   ar   div   log   

原文地址:http://www.cnblogs.com/iois/p/3940121.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!