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(以前写的)
Permutations【python】的更多相关文章
- 【Python②】python之首秀
第一个python程序 再次说明:后面所有代码均为Python 3.3.2版本(运行环境:Windows7)编写. 安装配置好python后,我们先来写第一个python程序.打开IDLE (P ...
- 【python】多进程锁multiprocess.Lock
[python]多进程锁multiprocess.Lock 2013-09-13 13:48 11613人阅读 评论(2) 收藏 举报 分类: Python(38) 同步的方法基本与多线程相同. ...
- 【python】SQLAlchemy
来源:廖雪峰 对比:[python]在python中调用mysql 注意连接数据库方式和数据操作方式! 今天发现了个处理数据库的好东西:SQLAlchemy 一般python处理mysql之类的数据库 ...
- 【python】getopt使用
来源:http://blog.chinaunix.net/uid-21566578-id-438233.html 注意对比:[python]argparse模块 作者:limodou版权所有limod ...
- 【Python】如何安装easy_install?
[Python]如何安装easy_install? http://jingyan.baidu.com/article/b907e627e78fe146e7891c25.html easy_instal ...
- 【Python】 零碎知识积累 II
[Python] 零碎知识积累 II ■ 函数的参数默认值在函数定义时确定并保存在内存中,调用函数时不会在内存中新开辟一块空间然后用参数默认值重新赋值,而是单纯地引用这个参数原来的地址.这就带来了一个 ...
- 【Python】-NO.97.Note.2.Python -【Python 基本数据类型】
1.0.0 Summary Tittle:[Python]-NO.97.Note.2.Python -[Python 基本数据类型] Style:Python Series:Python Since: ...
- 【Python】-NO.99.Note.4.Python -【Python3 条件语句 循环语句】
1.0.0 Summary Tittle:[Python]-NO.99.Note.4.Python -[Python3 条件语句 循环语句] Style:Python Series:Python Si ...
- 【Python】-NO.98.Note.3.Python -【Python3 解释器、运算符】
1.0.0 Summary Tittle:[Python]-NO.98.Note.3.Python -[Python3 解释器] Style:Python Series:Python Since:20 ...
随机推荐
- Spring MVC遭遇checkbox的问题解决方式
Spring MVC遭遇checkbox的问题是:当checkbox全不选时候,则该checkbox域的变量为null,不能动态绑定到spring的controller方法的入參上,并抛出异常. 解决 ...
- Json在asp.net开发中的应用
一.asp.net后台返回Json数据,前台js解析 在后台读取数据,并手动封装成Json格式: public ContentResult getUsersByOrgId(int Id) { Data ...
- 读取jar包里面的文件
一.最近做项目的时候,师兄要求读取jar包里面的java文件.在网上查了各种文件以后,终于完成了,在这里和各位朋友分享一下. (一)找到jar包所在的位置. String path="XXX ...
- windows cmd: 打开windows系统程序或服务的常见命令
Windows常用CMD命令 http://www.cnblogs.com/sbaicl/archive/2013/03/05/2944001.html 其实查找Windows自带程序的命令行很简单, ...
- CSS3 总结-1
新的边框属性 属性 描述 CSS border-image 设置所有 border-image-* 属性的简写属性. 3 border-radius 设置所有四个 border-*-radius 属性 ...
- python成长之路11
一.线程: 创建线程有两种方式(本质是一样的,创建好线程之后,cpu调度创建好的线程时执行的其实是Thread的run()方法): import threading def f1(args):prin ...
- python笔记之字符串
列表,元组,字符串的相互转换: 将字符串转换为序列和元组: >>> s="hello" >>> list(s)['h', 'e', 'l', ' ...
- Linux(CentOS或RadHat)下MySQL源码安装
安装环境: CentOS6.3 64位 软件: Mysql-5.6 所需包: gcc/g++ :MySQL 5.6开始,需要使用g++进行编译.cmake :MySQL 5.5开始,使用cmake进 ...
- Xamarin.Android开发实践(四)
原文:Xamarin.Android开发实践(四) Xamarin.Android下获取与解析JSON 一.新建项目 1.新建一个Android项目,并命名为为NetJsonList 2.右击引用,选 ...
- MFC内部结构剖析
//////////////////////////////////////////////////////////////////////////////////////////MFC程序的执行顺序 ...