31. Next Permutation
来源:互联网 发布:电脑版淘宝微淘在哪里 编辑:程序博客网 时间:2024/05/22 14:30
题意:就是生成比这个排列大的下一个排列,如果排列已经最大了,就直接生成最小的下一个排列。
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
思路:对于这一类排列的问题,都是有一个大概的思路的,都有一点像递归或者动态规划的感觉,首先,找到最长的非增后缀,然后将后缀的前一位设为标志位,再从后缀中从后往前地找出第一个大于标志位的数,把它和标志位调换顺序,最后把后缀反转一下就ok了。其实就是相当于找出临界的后缀,再对应改变前一位,之后将后缀化为最小的结果。这题具体思路可以看这个思路。
class Solution(object): def nextPermutation(self, nums): """ :type nums: List[int] :rtype: void Do not return anything, modify nums in-place instead. """ i, j = len(nums)-2, len(nums)-1 while nums[i+1] <= nums[i] and i >= 0: i -= 1 if i < 0: nums = nums[::-1] else: while nums[i] >= nums[j] and j > 0: j -= 1 nums[i], nums[j] = nums[j], nums[i] nums[i+1:] = nums[len(nums)-1:i:-1]
0 0
- [LeetCode]31.Next Permutation
- LeetCode --- 31. Next Permutation
- [Leetcode] 31. Next Permutation
- LeetCode 31.Next Permutation
- 31. Next Permutation
- [leetcode] 31.Next Permutation
- [leetcode] 31. Next Permutation
- *LeetCode 31. Next Permutation
- 31. Next Permutation
- Leetcode 31. Next Permutation
- 31. Next Permutation
- 31. Next Permutation
- 31. Next Permutation
- leetcode 31. Next Permutation
- 31. Next Permutation LeetCode
- 31. Next Permutation
- 31. Next Permutation
- 31. Next Permutation
- ORA-06553: PLS-306
- 微信开发之百度地图API学习(一)
- MySQL性能剖析工具(pt-query-digest)
- android apk应用系统集成变异错误问题
- Ibatic Cache 使用实例
- 31. Next Permutation
- Android开发之jni串口
- mysql 函数、存储过程、视图、触发器
- Bidirectional OneToOne association lazy loading support
- acm C小加 之 随机数
- android应用增量升级
- 基于winPcap的动态内容阻断
- mysql学习之-逻辑架构
- NIST 函数手册