Permutations【python】

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(以前写的)

时间: 2024-08-10 00:04:41

Permutations【python】的相关文章

【Python】SyntaxError: Non-ASCII character '\xe8' in file

遇到的第一个问题: SyntaxError: Non-ASCII character '\xe8' in file D:/PyCharmProject/TempConvert.py on line 2, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details 原因:注释里面出现了中文,而 Python 支持的 ASCII 码无中文. 解决方法:在头文件中添加如下代码: # -*- coding:

【python】禁止print输出换行的方法

print后用一个逗号结尾就可以禁止输出换行,例子如下 >>> i=0 >>> while i < 3: print i i+=1 0 1 2 禁止输出换行后效果如下: >>> i=0 >>> while i < 3: print i, i+=1 0 1 2 [python]禁止print输出换行的方法,布布扣,bubuko.com

【python】chr与ord函数的使用

ord()是将已知字母转换成其顺序值: chr()是将已知字母的顺序至转换成其对应的字母 >>> ord("a") 97 >>> ord("A") 65 >>> chr(97) 'a' >>> chr(65) 'A' [python]chr与ord函数的使用,布布扣,bubuko.com

【python】ipython与python的区别

[python]ipython与python的区别 (2014-06-05 12:27:40) 转载▼   分类: Python http://mba.shengwushibie.com/itbook/BookChapter.asp?id=8745 http://www.cnblogs.com/yangze/archive/2011/07/11/2103040.html http://matrix.42qu.com/10735149 http://www.cnblogs.com/weishun/

【python】字符遍历

Python为我们提供了很多便捷的方式去遍历一个字符串中的字符.比如,将一个字符串转换为一个字符数组(列表): theList = list(theString) 同时,我们可以方便的通过for语句进行遍历: for c in theString:        do_something_with(c) map函数用法: 第一个参数接收一个函数名,第二个参数接收一个可迭代对象 lt = [1, 2, 3, 4, 5, 6] def add(num): return num + 1 rs = ma

【Python】用Python的“结巴”模块进行分词

之前都是用计算所的分词工具进行分词,效果不错但是比较麻烦,最近开始用Python的"结巴"模块进行分词,感觉非常方便.这里将我写的一些小程序分享给大家,希望对大家有所帮助. 下面这个程序是对一个文本文件里的内容进行分词的程序:test.py #!/usr/bin/python #-*- encoding:utf-8 -*- import jieba #导入jieba模块 def splitSentence(inputFile, outputFile): fin = open(input

【Python】Python获取命令行参数

有时候需要用同一个Python程序在不同的时间来处理不同的文件,此时如果老是要到Python程序中去修改输入.输出文件名,就太麻烦了.而通过Python获取命令行参数就方便多了.下面是我写得一个小程序,希望对大家有所帮助. 比如下面一个程序test.py是通过接受命令行两个参数,并打印出这两个参数. import sys #需导入sys模块 print sys.argv[1], sys.argv[2] #打印出从命令行接受的两个参数 Linux下运行:python test.py Hello P

【Python】定位一组元素、

前几天生病加懒惰 TAT ========================================================================== 1.getAttribute()方法是一个函数.它只有一个参数--你打算查询的属性的名字: 2.http://www.cnblogs.com/fnng/p/3190966.html 注意路径 3. [Python]定位一组元素.,布布扣,bubuko.com

【Python】入门 list有些不懂

#-*- coding: cp936 -*-  #首行加这个 代码里就可以加注释 raw_input("Press Enter Exit")  #最后一行加这个,可以直接点击脚本运行脚本 l[a:b] = [c] #即用右边的[c]完全替换a到b-1个元素 http://www.cnblogs.com/zhengyuxin/articles/1938300.html python list 操作 http://blog.csdn.net/sunvince/article/details