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 and use only constant 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

思路


这道题最直观的思路就是直接将数组先进行排序,然后此次输出排序组合,知道找到当前的排列并输出下一个排列。但是时间复杂度较高。时间复杂度为O(n!),空间复杂度为O(n).

  当然这不是最优的解法,还有一种是利用排列的规律对数组进行变化,这样可以直接得到下一个排列。方法是我们从右边开始向前查找,知道找到满足a[i-1] < a[i] 的情况,然后我们在a[i]开始查找,知道找到一个满足 a[ j+1] < a[i-1] < a[ j] 的情况。最后对a[i]到尾部进行反转。得到下一个排列。时间复杂度为O(n),空间复杂度为O(1)。

图示


   ·

   

   

解决代码


 class Solution:
def nextPermutation(self, nums):
"""
:type nums: List[int]
:rtype: void Do not return anything, modify nums in-place instead.
"""
length = len(nums)
i = length - 2
while i >=0 and nums[i+1] <= nums[i]: #找到第一个nums[i+1] > nums[i]的下标
i -= 1
if i >= 0: # 如果i等于0,表示没有找到。直接将数组反转就可以得到结果。
j = length -1 # 从最后一个数字开始查找
while j>=0 and nums[i] >= nums[j]: # 知道找到第一个满足 nums[j] > nums[i]的下标
j -= 1
nums[i], nums[j] = nums[j], nums[i] # 交换两个位置 start = i + 1
end = length -1
while start < end: # 对i下标后面的进行反转,得到结果。
nums[start], nums[end] = nums[end], nums[start]
start += 1
end -= 1

【LeetCode每天一题】Next Permutation(下一个排列)的更多相关文章

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

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

  2. [LeetCode] 31. Next Permutation 下一个排列

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

  3. Leetcode题库——31.下一个排列

    @author: ZZQ @software: PyCharm @file: nextPermutation.py @time: 2018/11/12 15:32 要求: 实现获取下一个排列的函数,算 ...

  4. lintcode:next permutation下一个排列

    题目 下一个排列 给定一个整数数组来表示排列,找出其之后的一个排列. 样例 给出排列[1,3,2,3],其下一个排列是[1,3,3,2] 给出排列[4,3,2,1],其下一个排列是[1,2,3,4] ...

  5. Next Permutation 下一个排列

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

  6. 031 Next Permutation 下一个排列

    实现获取下一个排列函数,这个算法需要将数字重新排列成字典序中数字更大的排列.如果不存在更大的排列,则重新将数字排列成最小的排列(即升序排列).修改必须是原地的,不开辟额外的内存空间.这是一些例子,输入 ...

  7. [leetcode]31. Next Permutation下一个排列

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

  8. ACM_下一个排列

    The Next Permutation Time Limit: 2000/1000ms (Java/Others) Problem Description: For this problem, yo ...

  9. LeetCode 31. 下一个排列(Next Permutation)

    题目描述 实现获取下一个排列的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列. 如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列). 必须原地修改,只允许使用额外常 ...

随机推荐

  1. 如何将MP3录音转文字

    相信很多人都有电话录音的习惯,因为这样可以记录下很多重要的信息.那么当我们通过录音将一些重要的信息记录下来后,我们应该怎样将这些录音文件转换成文字进行记录呢?下面我们就一起来看一下吧. 操作步骤: 步 ...

  2. sql优化的方法

    参考:https://blog.csdn.net/jie_liang/article/details/77340905 11.不要写一些没有意义的查询,如需要生成一个空表结构: select col1 ...

  3. HDU 6228 - Tree - [DFS]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6228 Time Limit: 2000/1000 MS (Java/Others) Memory Li ...

  4. [No0000109]Git2/9-安装Git

    最早Git是在Linux上开发的,很长一段时间内,Git也只能在Linux和Unix系统上跑.不过,慢慢地有人把它移植到了Windows上.现在,Git可以在Linux.Unix.Mac和Window ...

  5. WebStorm中配置node.js(Windows)

    WebStorm中配置node.js(Windows) 一.node 1.下载安装包 32 位 : https://nodejs.org/dist/v4.4.3/node-v4.4.3-x86.msi ...

  6. javascript中call、apply、bind详解

    1.apply和call的区别在哪里 2.什么情况下用apply,什么情况下用call 3.apply的其他巧妙用法(一般在什么情况下可以使用apply) 我首先从网上查到关于apply和call的定 ...

  7. ArcEngine二次开发,TOCControl控件上使用contextMenuStrip

    右键菜单,在二次开发中很实用,以前没用过,最近通过一本书了解到,一直想找这么一个控件来用. 一般的控件,将contextMenuStrip控件拖到所依托的控件上,然后输入自己想要的几个功能.  在所依 ...

  8. 类Unix如何查看mysql的配置文件my.cnf

    mysql 配置文件 my.cnf是MySQL启动时加载的配置文件,一般会放在MySQL的安装目录中,用户也可以放在其他目录加载. 安装MySQL后,系统中会有多个my.cnf文件,有些是用户测试的. ...

  9. 转:eval(data)和eval("("+data+")")

    http://www.w3school.com.cn/jsref/jsref_eval.asp JavaScript eval() 函数:eval() 函数可计算某个字符串,并执行其中的的 JavaS ...

  10. LeetCode 292 Nim Game 解题报告

    题目要求 You are playing the following Nim Game with your friend: There is a heap of stones on the table ...