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 ...
随机推荐
- GCD自己做的一些简单总结
GCD总结 GCD Grand Central Dispatch 牛逼的中枢调度器 GCD中各种队列的执行效果 想看线程 必须是异步函数 并且不是主队列 注意:使用sync函数往当前串行队列添 ...
- Ants(思维)
Ants Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 12893 Accepted: 5637 Description ...
- find the most comfortable road(并差集,找差值最小的权值)
find the most comfortable road Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- Java程序猿JavaScript学习笔记(2——复制和继承财产)
计划和完成在这个例子中,音符的以下序列: Java程序猿的JavaScript学习笔记(1--理念) Java程序猿的JavaScript学习笔记(2--属性复制和继承) Java程序猿的JavaSc ...
- 解决shell脚本中 echo 怎么写入换行到文件
測试环境:ubuntu12.04 LTS版本号 echo >> file.txt就可以, 这样的方法对于脚本开头是bash和sh效果都一样, 而echo -e >> file. ...
- C# inherit
Case:class A has a construct. class B is inherit from class A and B also has a construct. What's the ...
- 在 Windows系统中编译node.js 源代码
Node.js 在 Windows 下只能通过 Microsoft Visual Studio 编译,因此你需要首先安装 Visual Studio 或者免费的 Visual Studio Expre ...
- javascript 横向下拉菜单演示
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh-CN"><head><me ...
- 伪静态 apache重写
mod_rewrite是Apache的一个非常强大的功能,它可以实现伪静态页面 下面我详细说说它的使用方法 A.检测Apache是否支持mod_rewrite 通过php提供的phpinfo()函数查 ...
- s3c2440栈分配情况(fl2440裸机 stack)
//2440INIT.S ;The location of stacks UserStack EQU (_STACK_BASEADDRESS-0x3800) ;0x33ff4800 ~ SVCStac ...