[Leetcode][Python]46: Permutations
# -*- coding: utf8 -*-
'''
__author__ = 'dabay.wang@gmail.com' 46: Permutations
https://leetcode.com/problems/permutations/ Given a collection of numbers, return all possible permutations.
For example,
[1,2,3] have the following permutations:
[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1]. === Comments by Dabay===
DFS.
注意的问题是,
加入到res结果集中的时候,做一个l的拷贝。
DSF之后恢复l的状态。
''' class Solution:
# @param num, a list of integer
# @return a list of lists of integers
def permute(self, num):
res = []
self.DFS(res, [], num)
return res def DFS(self, res, l, nums):
if len(nums) == 0:
res.append(list(l))
for i in xrange(len(nums)):
l.append(nums[i])
self.DFS(res, l, nums[:i] + nums[i+1:])
l.pop() def main():
sol = Solution()
num = [1, 2, 3]
print sol.permute(num) if __name__ == "__main__":
import time
start = time.clock()
main()
print "%s sec" % (time.clock() - start)
[Leetcode][Python]46: Permutations的更多相关文章
- 【LeetCode】46. Permutations 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 方法一:库函数 方法二:递归 方法三:回溯法 日期 题目地址:h ...
- LeetCode:46. Permutations(Medium)
1. 原题链接 https://leetcode.com/problems/permutations/description/ 2. 题目要求 给定一个整型数组nums,数组中的数字互不相同,返回该数 ...
- [Leetcode][Python]47: Permutations II
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 47: Permutations IIhttps://oj.leetcode. ...
- 【一天一道LeetCode】#46. Permutations
一天一道LeetCode系列 (一)题目 Given a collection of distinct numbers, return all possible permutations. For e ...
- LeetCode 【46. Permutations】
Given a collection of distinct numbers, return all possible permutations. For example,[1,2,3] have t ...
- 【LeetCode】46. Permutations (2 solutions)
Permutations Given a collection of numbers, return all possible permutations. For example,[1,2,3] ha ...
- 【LeetCode】47. Permutations II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:htt ...
- 【一天一道LeetCode】#47. Permutations II
一天一道LeetCode系列 (一)题目 Given a collection of numbers that might contain duplicates, return all possibl ...
- [LeetCode] 46. Permutations 全排列
Given a collection of distinct integers, return all possible permutations. Example: Input: [1,2,3] O ...
随机推荐
- C语言随笔_return答疑
1. 例子,看实例2-2.这道题有同学会问,那个return有什么用?这么讲吧,return是个英文单词,中文意思是“返回”,用在程序里也是返回的意思,返回啥呢?返回一个值.在func函数中,retu ...
- Entity Framework 技术参考:http://kb.cnblogs.com/zt/ef/
Entity Framework 技术参考:http://kb.cnblogs.com/zt/ef/
- 企业OA面临的问题,以及解决问题的推荐
现在的企业不管大小都趋于软件话,而办公用的OA软件更是成为了企业中不可获取的一环,一个好的软件能让企业发展的更加顺利,而一个不合适的软件可能让公司哀声怨道反而起了反作用! OA ...
- python2 和3的区别
__future__ 模块 Python 3.x引入一些Python2不兼容的关键字和函数,可以通过在 Python2 内置的模块 __future__ 导入.建议如果你想在代码中支持 Python3 ...
- easy ui 如何单个引用其中某个插件?
记录一下这个方法,前端时间一直在纠结这个问题,后来听一些前辈讲解后才恍然大悟,要单独引用某个插件,我们需要重视的是:easyloaer.js ,easy ui的下载包中也有easyloader的dem ...
- ibatis错误汇总
1) 错误:The prefix "context" for element "context:property-placeholder" is not bou ...
- nginx默认虚拟主机
server { listen 80 default_server; server_name localhost; index index.html index.htm index.php; root ...
- Android中进程生命周期的优先级
“我们不是生产者,我只是大自然的搬运工.” 学习Android最好的途径当然是强大的官方文档了,其中在Processes and Threads一节中对于进程生命周期淘汰优先级,有着详细的介绍.原文如 ...
- [zz]npm安装错误解决方法
错误: npm ERR! at Object.parse (native) npm ERR! at Packer.readRules (/usr/local/lib/node_modules/npm/ ...
- Java转换
1.如何将字符串String转化为整数int int i = Integer.parseInt(str); int i = Integer.valueOf(my_str).intValue(); ...