【LeetCode每天一题】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 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,23,2,1 → 1,2,31,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(下一个排列)的更多相关文章
- [LeetCode] Next Permutation 下一个排列
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- [LeetCode] 31. Next Permutation 下一个排列
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- Leetcode题库——31.下一个排列
@author: ZZQ @software: PyCharm @file: nextPermutation.py @time: 2018/11/12 15:32 要求: 实现获取下一个排列的函数,算 ...
- lintcode:next permutation下一个排列
题目 下一个排列 给定一个整数数组来表示排列,找出其之后的一个排列. 样例 给出排列[1,3,2,3],其下一个排列是[1,3,3,2] 给出排列[4,3,2,1],其下一个排列是[1,2,3,4] ...
- Next Permutation 下一个排列
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- 031 Next Permutation 下一个排列
实现获取下一个排列函数,这个算法需要将数字重新排列成字典序中数字更大的排列.如果不存在更大的排列,则重新将数字排列成最小的排列(即升序排列).修改必须是原地的,不开辟额外的内存空间.这是一些例子,输入 ...
- [leetcode]31. Next Permutation下一个排列
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- ACM_下一个排列
The Next Permutation Time Limit: 2000/1000ms (Java/Others) Problem Description: For this problem, yo ...
- LeetCode 31. 下一个排列(Next Permutation)
题目描述 实现获取下一个排列的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列. 如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列). 必须原地修改,只允许使用额外常 ...
随机推荐
- AJAX里使用的弹窗样式 tanchuang.js tanchuang.css
tanchuang.js // 每个弹窗的标识 var x =0; var idzt = new Array(); var Window = function(config){ //ID不重复 idz ...
- (domain)域名协议
https://jingyan.baidu.com/article/2c8c281df0afd00008252aa7.html
- Linux系统下升级Python版本步骤(suse系统)
Linux系统下升级Python版本步骤(suse系统) http://blog.csdn.net/lifengling1234/article/details/53536493
- Mysql thread 与 OS thread
测试环境信息如下: OS:Ubuntu 16.04 LTS Mysql:Mysql 5.7.18,使用docker images运行的实例 Mysql如何处理client请求 在Mysql中,连接管理 ...
- ionic使用cordova插件中的Screenshot截图分享功能
需要实现操作,考试完成后需要将成绩生成一张图片,分享出去, import { Screenshot } from '@ionic-native/screenshot'; constructor(pri ...
- iOS中类、元类、isa详解
类相信大家都知道是什么,如果看过runtime的源码或者看过相关的文章对isa肯定也不陌生,不过元类(meta class)大家可能就比较陌生了.不过大家也不要担心,我会细细道来,让大家明白它到底是个 ...
- iOS-静态库,动态库,framework,bundle浅析(四)
1. 创建bundle,如图,点击 + ,弹出选择框, macOS 下的Framework & Library ,点击bundle,输入bundle的名字,然后点击 finish. 图 ...
- [others]tinycore/microcore
https://zh.wikipedia.org/wiki/Tiny_Core_Linux https://github.com/zeit/micro
- [skill][funny] 一个很厉害的for循环
int DSSL_MoveServerToMissingKeyList( DSSL_Env* env, DSSL_ServerInfo* si ) { DSSL_ServerInfo** new_se ...
- javaweb连接数据库并完成增删改查
一.连接数据库 1.mysql数据库的安装和配置 在网上找到了篇关于mysql的安装详细说明,供读者自己学习 https://www.jb51.net/article/23876.htm 2.mysq ...