LeetCode 31 Next Permutation 寻找一个数字序列的比他大的最小序列
来源:互联网 发布:澄海网店美工培训 编辑:程序博客网 时间:2024/04/29 21:56
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, do not allocate 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
参考文章:http://www.2cto.com/kf/201501/372527.html
代码如下:
public void nextPermutation(int[] nums) { int le = nums.length-2; int len = nums.length; int i =le; for ( i = le; i >= 0; i--) {if(nums[i+1]>nums[i]){for (int j = len-1; j >= i; j--) {if (nums[j]>nums[i]) {int temp = nums[i];nums[i] = nums[j];nums[j] = temp;reverse(nums,i+1,len-1);return ;}}}} reverse(nums,0,len-1); return ; } public void reverse(int[] nums,int i,int j){ while(i<j){ int temp = nums[i]; nums[i]= nums[j]; nums[j] = temp; i++; j--; }
0 0
- LeetCode 31 Next Permutation 寻找一个数字序列的比他大的最小序列
- 【leetcode】31. Next Permutation 数字序列的所有组合中比给定串大的下一个最小的串
- Next Permutation(寻找字典序比输入大1的序列)
- LeetCode Next Permutation 生成下一个序列
- 寻找一个序列中第n大的元素
- 快速维护一个排列两边比他大的数字的位置
- 给定一个数字,求比它大的最小的非重复数字
- Permutation Sequence 求第k个的排列序列 @LeetCode
- 给定一个数字序列,输出一个最长的子序列
- python求解比一个给定的数字大的最小对称数
- LeetCode 31: Next Permutation
- [leetcode 31] Next Permutation
- LeetCode(31) Next Permutation
- LeetCode---(31) Next Permutation
- leetcode 31 -- Next Permutation
- leetcode 31: Next Permutation
- LeetCode(31) Next Permutation
- LeetCode-31 Next Permutation
- Delete Node in a Linked List 单链表删除节点
- log4j日志配置
- try catch finally中含有return时的处理步骤
- viewpager setpagechangelistener解释与应用到如微信的页面转换的渐变效果
- NS2网络仿真环境的搭建和使用
- LeetCode 31 Next Permutation 寻找一个数字序列的比他大的最小序列
- 总线设备驱动模型
- Aircrack-ng之Airmon-ng命令
- HTTP协议
- 十年无涯
- &(与运算)、|(或运算)、^(异或运算)的本质理解
- Leetcode: Word Break
- iOS的socket开发基础
- MySQL开发规范与使用技巧总结