Algorithms—31.Next Permutation
来源:互联网 发布:电子笔记 知乎 编辑:程序博客网 时间:2024/06/05 23:30
思路:首先找到最后一段从大到小的排序子列,然后将其反向,找到这个子列开头的数字,与该子列比较,找到第一个比他大的数字,交换。
public class Solution { public void nextPermutation(int[] nums) { if (nums.length>1) { int a=0; for (int i = 1; i < nums.length; i++) { if (nums[i]>nums[i-1]) { a=i; } } for (int i = a; i < (a+nums.length)/2; i++) { int k=nums[i]; nums[i]=nums[nums.length-i+a-1]; nums[nums.length-i+a-1]=k; } if (a>0) { for (int i = a; i < nums.length; i++) { if (nums[a-1]<nums[i]) { int k=nums[i]; nums[i]=nums[a-1]; nums[a-1]=k; break; } } }} }}
耗时:348ms,中下游。
0 0
- Algorithms—31.Next Permutation
- Leetcode——31. Next Permutation
- [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
- Ubuntu中配置GitLab版本管理系统
- 关于const char *p 、char const*p 、char *const p 的区别
- TS流
- msh格式解析
- 测试
- Algorithms—31.Next Permutation
- leetcode之路003 Longest Substring Without Repeating Characters
- 客户端传值到服务器中文乱码
- 该 App 的 Info.plist 不能包含阻碍在任何 iOS 设备上打开此 App 的 UIRequiredDeviceCapabilities 键值
- 解决Picasso加载图片出现闪烁的问题
- #笔记#圣思园 JavaWeb 第59讲——使用Firebug调试JS、JSUnit,XUnit
- 关于C语言中的预处理指令
- Objective-C NSNotificationCenter (通知)的使用方法
- Android 适配问题