[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 ...
随机推荐
- Unix/Linux环境C编程入门教程(33) 命令和鼠标管理用户和组
Linux是一个多用户.多任务的实时操作系统,允许多人同时访问计算机, 并同时运行多个任务.UNIX系统具有稳定.高效.安全.方便.功能强大等诸多优点,自20世纪70年代开始便运行在许多大型和小型计算 ...
- < IOS > 文件中 某个类设置ARC,或者非ARC
用-fno-objc-arc标记来禁用在ARC工程那些不支持ARC的文件的ARC用-fobjc-arc标记启用非ARC工程中支持ARC的文件 项目targets -> build phases ...
- DICOM:C-GET与C-MOVE对照剖析
背景: 之前专栏中介绍最多的两款PACS各自是基于dcmtk的dcmqrscp以及Orthanc.和基于fo-dicom的DicomService(自己开发的).该类应用场景都是针对于局域网,因此在使 ...
- socketpair的使用
socketpair函数概要例如以下:#include <sys/types.h>#include <sys/socket.h>int socketpair(int domai ...
- ubuntu 下安装伪分布式 hadoop
安装准备: (1)hadoop安装包:hadoop-1.2.1.tar.gz (2)jdk安装包:jdk-7u60-linux-i586.gz (3)要是须要eclipse开发的话 还须要eclips ...
- Unity 屏幕震动效果实现
要实现震屏效果其实并不难,所谓的震屏在PC端可以简单地理解为相机(MainCamera)的抖动. 代码实现如下: using UnityEngine; using System.Collections ...
- Mongodb中Sharding集群
随着mongodb数据量的增多,可能会达到单个节点的存储能力限制,以及application较大的访问量也会导致单个节点无法承担,所以此时需要构建集群环境,并通过sharding方案将整个数据集拆分成 ...
- NDK下 将Platinum SDK 编译成so库 (android - upnp)
Platinum UPnP SDK 是一个跨平台的C++库,利用该库,可以很容易就构建出DLNA/UPnP控制点(DLNA/UPnP Control Point)和DLNA/UPnP设备(DLNA/U ...
- LINQ 基本子句之二 join
Join子句据说可以实现3中连接关系. 1.内部连接——元素的连接关系必须同时满足被连接的两个数据源 2.分组连接 3.左外连接 1.最基本的,内部连接,类似于sql中inner join. 由于st ...
- @@IDENTITY在加触发器时返回错误的ID值
表ID是自增的,所以在添加时要查一下,之前是用@@IDENTITY来查,最近在加触发器时发现返回的会是在触发器中插入语句的数据ID值,上网找了下资料,发现是因为@@IDENTITY 将返回在当前会话中 ...