31 Next Permutation
来源:互联网 发布:pdf.js 获取总页数 编辑:程序博客网 时间:2024/06/05 06:34
/* * 思路:从右向左,长度不断增长,找到第一个逆序的数(数从大到小排序)例如1,1,5,3,2则当长度增长到4时 * 数列范围为:1,5,3,2,将5,3,2排序,然后找到第一个比1大的数,然后与1互换,则得到了结果1,2,1,3,5 */public class Solution { public void nextPermutation(int[] nums) { if(nums==null||nums.length==0) return; int len = nums.length; int f = len - 1; while(f>=0){ if(isSorted(nums, f, len)){ --f; }else{ Arrays.sort(nums, f+1, len); int r = f + 1; for(int k=f+1;k<len;++k){ if(nums[f]<nums[k]){ r = k; break; } } int tmp = nums[f]; nums[f] = nums[r]; nums[r] = tmp; return; } } Arrays.sort(nums); }public boolean isSorted(int[] nums, int beign, int end){int t = nums[beign];for(int i=beign+1;i<end;++i){if(t<nums[i]){return false;}}return true;}}
0 0
- LeetCode 31: Next Permutation
- [leetcode 31] Next Permutation
- LeetCode(31) Next Permutation
- LeetCode---(31) Next Permutation
- 31-m-Next Permutation
- leetcode 31 -- Next Permutation
- leetcode 31: Next Permutation
- Leecode#31 Next Permutation
- LeetCode(31) Next Permutation
- LeetCode-31 Next Permutation
- Leetcode#31||Next Permutation
- 31Next Permutation
- 31 Next Permutation
- LeetCode 31: Next Permutation
- [31]Next Permutation
- 31 Next Permutation
- 31、Next Permutation
- LeetCode(31)-Next Permutation
- SGU 438 The Glorious Karlutka River =) 动态网络流
- Servlet注册功能的实现
- u-boot-2009.11移植(适用于TQ2440和MINI2440)第一篇:建立开发板文件,测试编译环境
- HDU 1569 方格取数(2)(最大点权独立集)
- 类再生
- 31 Next Permutation
- 指针之指针数组
- 笔记本无线WiFi生成代码
- makefile
- CmpColorEx、FindMultiColor 命令详解
- Java程序员如何理解Python中的一些比较特殊的关键字
- poj 3259 Wormholes (bellman_ford)
- 哈夫曼树例子
- 树莓派2的内核编译与安装