[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 ...
随机推荐
- FileStream类
使用FileStream能够对对系统上的文件进行读.写.打开.关闭等操作.并对其他与文件相关的操作系统提供句柄操作,如管道,标准输入和标准输出.读写操作可以指定为同步或异步操作.FileStream对 ...
- 正则取页面图片URL和TABLE BackGround
/// <summary> /// 根据html文本返回url地址集合 /// </summary> /// <param name="sHtmlText&qu ...
- 《Programming WPF》翻译 目录
原文:<Programming WPF>翻译 目录 注:第1.2章我只做了笔记,没有翻译,请大家阅读时注意. 还有就是,这本书的英文版本下载:[O'Reilly] Programming ...
- bzoj1864 [Zjoi2006]三色二叉树
Description Input 仅有一行,不超过500000个字符,表示一个二叉树序列. Output 输出文件也只有一行,包含两个数,依次表示最多和最少有多少个点能够被染成绿色. Sample ...
- Subsets 解答
Question Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a su ...
- House Robber 解答
Question You are a professional robber planning to rob houses along a street. Each house has a certa ...
- C:\Program Files (x86)\Common Files\microsoft shared\TextTemplating\11.0
Generating Files with the TextTransform Utility \Program Files\Common Files\Microsoft Shared\TextTem ...
- ObjectOutputStream 追加写入读取错误
摘自http://blog.csdn.net/mitkey/article/details/50274543 问题描述: 用类ObjectOutputStream向文件写读对象时,碰到一个问题:新建一 ...
- 解决ios上微信无法捕获返回键按钮事件的问题
1 //匿名函数 $(function(){ getHistory(); var flag=false; setTimeout(function(){ flag=true },1000) window ...
- CF(441D Valera and Swaps)置换群
题意:1-n的一个排列, p2, ..., pn,f(p)的定义是此排列要交换最少的数对能够回到原排列1,2,3,4...n.给一个排列p.要将其变换成f值为m的排列,问至少要交换几个数对,并输出字典 ...