# -*- 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的更多相关文章

  1. 【LeetCode】46. Permutations 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 方法一:库函数 方法二:递归 方法三:回溯法 日期 题目地址:h ...

  2. LeetCode:46. Permutations(Medium)

    1. 原题链接 https://leetcode.com/problems/permutations/description/ 2. 题目要求 给定一个整型数组nums,数组中的数字互不相同,返回该数 ...

  3. [Leetcode][Python]47: Permutations II

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 47: Permutations IIhttps://oj.leetcode. ...

  4. 【一天一道LeetCode】#46. Permutations

    一天一道LeetCode系列 (一)题目 Given a collection of distinct numbers, return all possible permutations. For e ...

  5. LeetCode 【46. Permutations】

    Given a collection of distinct numbers, return all possible permutations. For example,[1,2,3] have t ...

  6. 【LeetCode】46. Permutations (2 solutions)

    Permutations Given a collection of numbers, return all possible permutations. For example,[1,2,3] ha ...

  7. 【LeetCode】47. Permutations II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:htt ...

  8. 【一天一道LeetCode】#47. Permutations II

    一天一道LeetCode系列 (一)题目 Given a collection of numbers that might contain duplicates, return all possibl ...

  9. [LeetCode] 46. Permutations 全排列

    Given a collection of distinct integers, return all possible permutations. Example: Input: [1,2,3] O ...

随机推荐

  1. 使用Qt编写服务器端程序(包括Http传输服务器端)的方法

    使用Qt编写客户端的程序的示例或demo较多,但是编写服务器端程序的demo很少.当然,服务器端的程序一般不需要带界面,这点我们可以理解.不过有些时候我们还是需要使用Qt编写一个简单的测试用的服务器代 ...

  2. Unattended Setup Software Components (无人值守安装软件组件)

    原文 http://social.technet.microsoft.com/Forums/windows/en-US/d4ad85b4-8342-4401-83ed-45cefa814ec5/una ...

  3. 【总结】OJ练习,进行的一些编程语言方面总结

    1.STL vector只有四个构造函数 ) explicit vector (const allocator_type& alloc = allocator_type()); fill () ...

  4. 搜索和搜索形式(SEARCHING and its forms)

    什么是搜索? 在计算机科学中,搜索就是在一个事物的集合中找到具有特定特征的一项的过程.这些集合中的元素可能是排好序的数据库中的记录,简单数组中的数据,文件中的文本,树中的节点,几何图形中的点和边或者是 ...

  5. C pointers

    指向整型数组指针int (*p)[10] = matrix;增加这个指针的值使它指向下一个整型数组 指向整型指针int *pi = &matrix[0][0];int *pi = &m ...

  6. Team Formation(思维)

    Team Formation Time Limit: 3 Seconds      Memory Limit: 131072 KB For an upcoming programming contes ...

  7. java的wait和notifyAll使用方法

    class Num { private int num; public int getNum() { return num; } public void setNum(int num) { this. ...

  8. CentOS 5.7 中文乱码问题解决方案

    一.安装中文支持: # yum install "@Chinese Support" 二.用 yum 安装中文字体 #yum install fonts-chinese.noarc ...

  9. Table生成Excel表格

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. Java web项目

    前言 本文目标:使用eclipse为IDE环境搭建一个基于maven的web项目,讲解搭建过程,项目结构,程序运行.调试和测试过程,并使用maven作为持续集成工具.     面向对象:转型java的 ...