原题地址:https://oj.leetcode.com/problems/next-permutation/

题意:

Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.

If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).

The replacement must be in-place, do not allocate extra memory.

Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand column.
1,2,3 → 1,3,2
3,2,1 → 1,2,3
1,1,5 → 1,5,1

解题思路:输出字典序中的下一个排列。比如123生成的全排列是:123,132,213,231,312,321。那么321的next permutation是123。下面这种算法据说是STL中的经典算法。在当前序列中,从尾端往前寻找两个相邻升序元素,升序元素对中的前一个标记为partition。然后再从尾端寻找另一个大于partition的元素,并与partition指向的元素交换,然后将partition后的元素(不包括partition指向的元素)逆序排列。比如14532,那么升序对为45,partition指向4,由于partition之后除了5没有比4大的数,所以45交换为54,即15432,然后将partition之后的元素逆序排列,即432排列为234,则最后输出的next permutation为15234。确实很巧妙。

代码:

class Solution:
# @param num, a list of integer
# @return a list of integer
def nextPermutation(self, num):
if len(num) <= 1: return num
partition = -1
for i in range(len(num)-2, -1, -1):
if num[i] < num[i+1]:
partition = i
break
if partition == -1:
num.reverse()
return num
else:
for i in range(len(num)-1, partition, -1):
if num[i] > num[partition]:
num[i],num[partition] = num[partition],num[i]
break
left = partition+1; right = len(num)-1
while left < right:
num[left],num[right] = num[right],num[left]
left+=1; right-=1
return num

改进一点:

class Solution:
# @param num, a list of integer
# @return a list of integer
def nextPermutation(self, num):
if len(num) <= 1: return num
partition = -1
for i in range(len(num)-2, -1, -1):
if num[i] < num[i+1]:
partition = i
break
if partition == -1:
num.reverse()
return num
else:
for i in range(len(num)-1, partition, -1):
if num[i] > num[partition]:
num[i],num[partition] = num[partition],num[i]
break
num[partition+1:len(num)]=num[partition+1:len(num)][::-1]
return num

[leetcode]Next Permutation @ Python的更多相关文章

  1. LeetCode:60. Permutation Sequence,n全排列的第k个子列

    LeetCode:60. Permutation Sequence,n全排列的第k个子列 : 题目: LeetCode:60. Permutation Sequence 描述: The set [1, ...

  2. [LeetCode]题解(python):031-Next Permutation

    题目来源 https://leetcode.com/problems/next-permutation/ Implement next permutation, which rearranges nu ...

  3. [LeetCode]题解(python):060-Permutation Sequence

    题目来源 https://leetcode.com/problems/permutation-sequence/ The set [1,2,3,…,n] contains a total of n! ...

  4. [LeetCode] 60. Permutation Sequence 序列排序

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  5. [LeetCode] 567. Permutation in String 字符串中的全排列

    Given two strings s1 and s2, write a function to return true if s2 contains the permutation of s1. I ...

  6. [LeetCode] Palindrome Permutation II 回文全排列之二

    Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...

  7. [LeetCode] Palindrome Permutation 回文全排列

    Given a string, determine if a permutation of the string could form a palindrome. For example," ...

  8. [LeetCode] Next Permutation 下一个排列

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  9. [LeetCode]题解(python):125 Valid Palindrome

    题目来源 https://leetcode.com/problems/valid-palindrome/ Given a string, determine if it is a palindrome ...

随机推荐

  1. SmartSVN11 Mac版 注册码序列号

    Name=apipostAddress=1337 iNViSiBLE Str.Email=3257132998@qq.comFreeUpdatesUntil=2099-09-26LicenseCoun ...

  2. java中的PO,VO,TO,BO,DAO,POJO的解释

    java的(PO,VO,TO,BO,DAO,POJO)解释  O/R Mapping 是 Object Relational Mapping(对象关系映射)的缩写.通俗点讲,就是将对象与关系数据库绑定 ...

  3. 膨胀卷积与IDCNN

    Dilation 卷积,也被称为:空洞卷积.膨胀卷积. 一.一般的卷积操作: 首先,可以通过动态图,理解正常卷积的过程: 如上图,可以看到卷积操作. 对于CNN结构,通常包括如下部分: 输入层 (in ...

  4. Node.js用6行代码1个JS文件搭建一个HTTP静态服务器

    Node.js是一个基于Chrome的JavaScript运行时的用户以轻松构建快速.可扩展的网络应用平台. Node.js使用事件驱动.非阻塞I/ O模型,使它轻量级.高效和完美的适用于运行在分布式 ...

  5. SlickMaster.NET 开源表单设计器快速使用指南

    前言:在企业数据处理过程中,经常需要通过定制表单来输入业务数据.由于涉及的数据比较离散,并不同于ERP系统的紧密关联数据.假如由开发人员每个增加页面,工作量会比较大,后期后期的维护很升级也耗费时间和精 ...

  6. RC4加密算法的原理及实现

    RC4于1987年提出,和DES算法一样.是一种对称加密算法,也就是说使用的密钥为单钥(或称为私钥). 但不同于DES的是.RC4不是对明文进行分组处理,而是字节流的方式依次加密明文中的每个字节.解密 ...

  7. LPC-LINK 2 Board IO TABLE

  8. STM32F4 Timer simplified block diagram

    Timers TIM1 and TIM8 use 16-bit counters and are the most complex timers of all timers included in t ...

  9. oracle like 条件拼接

    (1) ibatis xml配置:下面的写法只是简单的转义 namelike '%$name$%' (2) 这时会导致sql注入问题,比如参数name传进一个单引号“'”,生成的sql语句会是:nam ...

  10. ThinkPHP 模型方法 setInc() 和 setDec() 使用详解

    对于数字字段的加减,可以直接使用 setInc() 与 setDec() 方法 ThinkPHP 内置了对统计数据(数字字段)的更新方法: setInc():将数字字段值增加 setDec():将数字 ...