52. 下一个排列

中文English

给定一个整数数组来表示排列,找出其之后的一个排列。

Example

例1:

输入:[1]
输出:[1]

例2:

输入:[1,3,2,3]
输出:[1,3,3,2]

例3:

输入:[4,3,2,1]
输出:[1,2,3,4]

Notice

排列中可能包含重复的整数

遇到这种题目,只能自己找找规律:

1 5 2 3 4 / /
1 5 2 4 3 (2 1) / \ / \
1 2 3 4 5 / down swap 2 only
5 4 3 2 1 \ up ==> 极端情形(独一) (1)场景
5 2 3 1 0 \ / \ up down ==> swap(min2(down), find greater than min2), then sort left (2)场景

基本上场景就是看你数据考虑是否全面。

通过观察总结起来的做法就是:

class Solution:
"""
@param nums: A list of integers
@return: A list of integers
"""
def nextPermutation(self, nums):
# write your code here
n = len(nums)
i = n-1
while i > 0 and nums[i] <= nums[i-1]:
i -= 1 if i == 0:
return nums[::-1] assert nums[i] > nums[i-1] greater_index = i
for j in range(i+1, n):
if nums[j] > nums[i-1]:
greater_index = j
else:
break assert nums[greater_index] > nums[i-1] nums[greater_index], nums[i-1] = nums[i-1], nums[greater_index] return nums[0:i] + sorted(nums[i:])

  

dfs 之 下一个排列的更多相关文章

  1. 排列算法汇总(下一个排列,全排列,第K个排列)

    一.下一个排列 首先,STL提供了两个用来计算排列组合关系的算法,分别是next_permutation和prev_permutation. next_permutation(nums.begin() ...

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

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

  3. lintcode:next permutation下一个排列

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

  4. C++构造 下一个排列 的函数

    今天围观刘汝佳神犇的白书发现了一个好用的函数: next_permutation(); 可以用于可重, 或者不可重集, 寻找下一个排列. 时间复杂度尚不明. //适用于不可重和可重集的排列. # in ...

  5. LinkCode 下一个排列、上一个排列

    http://www.lintcode.com/zh-cn/problem/next-permutation-ii/# 原题 给定一个若干整数的排列,给出按正数大小进行字典序从小到大排序后的下一个排列 ...

  6. Next Permutation 下一个排列

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

  7. [Swift]LeetCode31. 下一个排列 | Next Permutation

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

  8. LeetCode(31): 下一个排列

    Medium! 题目描述: (请仔细读题) 实现获取下一个排列的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列. 如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列) ...

  9. 【LeetCode每天一题】Next Permutation(下一个排列)

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

随机推荐

  1. 缓存穿透 & 缓存击穿 & 缓存雪崩

    参考文档: 缓存穿透和缓存失效的预防和解决:https://blog.csdn.net/qq_16681169/article/details/75138876 缓存穿透 缓存穿透是指查询一个一定不存 ...

  2. 第2课 auto类型推导(1)

    第2课 auto类型推导(1) 一.auto类型推导 (一)与模板类型推导映射关系 1.auto类型推导与模板类型推导可以建立一一映射关系,它们之间存在双向的算法变换.auto扮演模板中T的角色,而变 ...

  3. 【IntelliJ IDEA学习之五】IntelliJ IDEA 搭建项目

    版本:IntelliJIDEA2018.1.4 一.同一窗口展示多个应用(弊端:耗内存) idea没有eclipse workspace的概念,如果想在同一窗口显示多个应用,可以按照如下方式来做:1. ...

  4. ASP.Net Core使用分布式缓存Redis从入门到实战演练

    一.课程介绍 人生苦短,我用.NET Core!缓存在很多情况下需要用到,合理利用缓存可以一方面可以提高程序的响应速度,同时可以减少对特定资源访问的压力.  所以经常要用到且不会频繁改变且被用户共享的 ...

  5. jvm jdk jre 关系

    JDK : Java Development ToolKit(Java开发工具包).JDK是整个JAVA的核心,包括了Java运行环境(Java Runtime Envirnment),一堆Java工 ...

  6. JAVA数学函数与常量

    在JAVA中,没有幂运算,因此需要借助于Math类的pow方法. double y = Math.pow(x,a) Math类提供了一些常用的三角函数: Math.sin Math.cos Math. ...

  7. HTML5 Canvas实战之刮奖效果【转】

    开源项目地址:https://github.com/artwl/Lottery 作者博客地址:http://www.cnblogs.com/jscode/p/3580878.html 谢谢浏览!

  8. ASP.NET Core应用程序的参数配置及使用(转载)

    本文结构 提前准备 参数配置方式 appsettings.json 环境变量 命令行参数 在控制器中使用配置参数 注入IConfiguration对象 注入IOptions对象 总结 应用程序的开发不 ...

  9. https相关知识总结

    从园子里看到很多讲解不错的文章,将链接放到这里,备忘 浅析数字证书:https://www.cnblogs.com/hyddd/archive/2009/01/07/1371292.html

  10. String.Operation

    // 字符串切割 StringField.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);