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. springMVC参数传递实例

    最好所有的post请求用postman这个工具就好了. postman传对象

  2. h5 . css入门 2.CSS基础

    CSS基础 学习目标 1.CSS简介 2.CSS语法 3.样式的创建 4.两种引入外部样式表的区别 5.样式表的优先级和作用域 6.CSS选择器 7.选择器的权重 8.浮动属性的简单应用 9.HTML ...

  3. 网龙“MAD技术论坛”在榕举办 200余位技术人才共话“改变教育”

    9月16日,由网龙网络公司主办.msup协办的“MAD技术论坛”在榕举办,来自美国.香港.苏州等地的技术大牛受邀来到福州,围绕“Make a difference to education”这一论坛主 ...

  4. POJ 2442 - Sequence - [小顶堆][优先队列]

    题目链接:http://poj.org/problem?id=2442 Time Limit: 6000MS Memory Limit: 65536K Description Given m sequ ...

  5. [No0000126]SSL/TLS原理详解与WCF中的WS-Security

    SSL/TLS作为一种互联网安全加密技术 1. SSL/TLS概览 1.1 整体结构 SSL是一个介于HTTP协议与TCP之间的一个可选层,其位置大致如下: SSL:(Secure Socket La ...

  6. [skill][c][ld][gcc] 明确指定gcc在链接时明确使用静态库

    通常,我们这样链接: gcc -o a.out -L/usr/lib/ -ltest a.o 这样它会链接动态库 libtest.so,  可是有时候我们想在静态库与动态库同时存在时,优先使用静态库. ...

  7. oracle 监听报错the information provided for this listener is currently in use by other software on this computer

    use another port number: the information provided for this listener is currently in use by other sof ...

  8. LeetCode 852 Peak Index in a Mountain Array 解题报告

    题目要求 Let's call an array A a mountain if the following properties hold: A.length >= 3 There exist ...

  9. 《HTTP - http2.0》

    推荐一首歌 - <卡路里>火箭101 杨超越真的只唱了一句! PS: 这章主要讲述了在 HTTP 发展之后的,一些扩展协议和补充,也会在这个章节对这几种协议和补充做一个比较. 1:HTTP ...

  10. 目标检测(3)-SPPNet

    Spatial Pyramid Pooling in Deep Convolutional Networks for Visual Recognition 文章地址:https://arxiv.org ...