Given a collection of distinct 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],
[3,2,1]
]

运用递归。 1234为例子

for  i in 1234:

  1  +  234(的全排列)

  2  +  134(的全排列)

  3  +  124(的全排列)

  4   + 123 (的全排列)

对应程序的17行

 class Solution(object):
def __init__(self):
self.res = [] def permute(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
self.help(nums, 0, len(nums)) return self.res def help(self, a, lo, hi):
if(lo == hi):
self.res.append(a[0:hi])
for i in range(lo, hi):
self.swap(a, i, lo)
self.help(a, lo + 1, hi)
self.swap(a, i, lo)
def swap(self, a, i, j):
temp = a[i]
a[i] = a[j]
a[j] = temp

非递归

 class Solution(object):

     def permute(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
nums = sorted(nums.copy())
res = [nums.copy()] n = self.next_permute(nums)
while True: if n is None:
break
res.append(n)
n = self.next_permute(n.copy()) return res def next_permute(self, a):
i = -1
for index in range(0, len(a) - 1)[::-1]:
if(a[index] < a[index + 1]):
i = index
break
if i==-1:
return None
min_i = a.index(min([k for k in a[i+1:] if k >a[i]]))
self.swap(a, i, min_i)
an = a[:i + 1] + self.revers(a[i + 1:])
return an def revers(self, x):
for i in range(int(len(x) / 2)):
temp = x[i]
x[i] = x[len(x) - i - 1]
x[len(x) - i - 1] = temp
return x def swap(self, a, i, j):
temp = a[i]
a[i] = a[j]
a[j] = temp

46. Permutations (全排列)的更多相关文章

  1. [LeetCode] 46. Permutations 全排列

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

  2. [leetcode]46. Permutations全排列(给定序列无重复元素)

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

  3. 46 Permutations(全排列Medium)

    题目意思:全排列 思路:其实看这题目意思,是不太希望用递归的,不过还是用了递归,非递归的以后再搞吧 ps:vector这玩意不能随便返回,开始递归方法用vector,直接到500ms,换成void,到 ...

  4. LeetCode - 46. Permutations

    46. Permutations Problem's Link -------------------------------------------------------------------- ...

  5. [CareerCup] 9.5 Permutations 全排列

    9.5 Write a method to compute all permutations of a string. LeetCode上的原题,请参加我之前的博客Permutations 全排列和P ...

  6. [Leetcode][Python]46: Permutations

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 46: Permutationshttps://leetcode.com/pr ...

  7. 46. Permutations 排列数

    46. Permutations 题目 Given a collection of distinct numbers, return all possible permutations. For ex ...

  8. 刷题46. Permutations

    一.题目说明 题目是46. Permutations,给一组各不相同的数,求其所有的排列组合.难度是Medium 二.我的解答 这个题目,前面遇到过类似的.回溯法(树的深度优先算法),或者根据如下求解 ...

  9. LeetCode 46 Permutations(全排列问题)

    题目链接:https://leetcode.com/problems/permutations/?tab=Description   Problem:给出一个数组(数组中的元素均不相同),求出这个数组 ...

随机推荐

  1. 用 HTML5+ payment方法支付宝支付遇到的坑

    用 HTML5+ payment方法碰到的第一个坑就是如果是支付宝的话签约那种支付方式. 因为 Dcloud的文档没有更新的原因你可以看到他们说的都是‘移动支付’,但是你去支付宝平台的时候看到的根本就 ...

  2. MyBitis(iBitis)系列随笔之二:类型别名(typeAliases)与表-对象映射(ORM)

    类型别名(typeAliases):     作用:通过一个简单的别名来表示一个冗长的类型,这样可以降低复杂度.    类型别名标签typeAliases中可以包含多个typeAlias,如下 < ...

  3. Hibernate_day04--HQL查询

    HQL查询 1 hql:hibernate query language,hibernate提供一种查询语言,hql语言和普通sql很相似,区别:普通sql操作数据库表和字段,hql操作实体类和属性 ...

  4. 『SharePoint 2010』Sharepoint 2010 Form 身份认证的实现(基于SQL)

    1:创建一个基于身份认证的应用程序(具体参见上篇基于AD) SQL-MembershipProvider 成员SQL-RoleManager 角色 2:修改管理中心,我们创建的应用程序,还有Web服务 ...

  5. Caused by: java.lang.ClassNotFoundException[android的终极解决错误]

    from:http://blog.csdn.net/changemyself/article/details/7861525 08-13 18:29:22.924: E/AndroidRuntime( ...

  6. JavaWeb配置错误页面

    我们在实际开发过程中经常会遇到程序出错的各种情况,比如最常见的404错误,500错误等等相关错误,服务器默认会将错误的信息直接显示在浏览器的页面上,如下图所示: 不管是谁如果看到这种情况的话,顿时就会 ...

  7. 使用Ansible自动配置Nginx服务

    1.首先安装好Ansible环境,具体步骤请见Ansible安装 2.先创建hosts文件(为后面编写脚本安装JDK做铺垫) [root@localhost /]# vi hosts [jdktest ...

  8. MySQL 监控指标

    为了排查问题,对数据库的监控是必不可少的,在此介绍下 MySQL 中的常用监控指标. 简介 MySQL 有多个分支版本,常见的有 MySQL.Percona.MariaDB,各个版本所对应的监控项也会 ...

  9. IE8数组不支持indexOf方法的解决办法

    在使用indexof方法之前加上以下代码就可以了. if (!Array.prototype.indexOf){           Array.prototype.indexOf = functio ...

  10. ArcGIS Server管理工具之批量发布动态地图服务工具.md

    友好阅读链接:(http://swj.me/2015/08/26/batchPublishtools/) update0918: 修复了创建链接文件时而出错的bug 修复了在24011的警告已处理的情 ...