【排列&字典序】Next Permutation
来源:互联网 发布:昆明网络推广哪家好 编辑:程序博客网 时间:2024/05/22 15:03
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
public class Solution { public void reverse(int []num, int i){ int j = num.length - 1; while(i < j){ int temp = num[i]; num[i] = num[j]; num[j] = temp; i++; j--; } } public void nextPermutation(int[] num) { int len = num.length; if(len <= 1) return ; int pos = len - 2; while(pos >=0 && num[pos+1] <= num[pos]) pos--; if(pos >= 0){ int i = len - 1; while(num[i] <= num[pos]) i--; int temp = num[pos]; num[pos] = num[i]; num[i] = temp; if(pos+1 < len) reverse(num, pos+1); } else{ Arrays.sort(num); } }}
0 0
- 【排列&字典序】Next Permutation
- LeetCode Next Permutation(字典序排列)
- [LeetCode]—Next Permutation (全排列字典序)
- POJ 3785 The Next Permutation 全排列字典序法
- 求字典序的下一个排列(对应lc的Next Permutation)
- 下一个排列(Next Permutation)
- Next Permutation 下一个排列
- Next Permutation 下一个排列
- Next Permutation -- LeetCode(下一个字典序)
- Next Permutation之字典序法
- 31.Next Permutation 下一个排列
- Next Permutation:已知某个排列求下个排列
- 第k个字典序全排列(kth permutation)
- LeetCode | Next Permutation(下一个排列)
- Next Permutation 寻找下一个排列数
- LeetCode 31 Next Permutation(下一个排列)
- [LeetCode]Next Permutation下一个排列问题
- LeetCode 31. Next Permutation(下一个排列)
- CSS3 Media Queries 实现响应式设计
- Linux 下升级python
- Spring 各个版本文档及jar包的下载地址
- SeekBar的使用(二):控制Button的缩放,移动及旋转
- HDU 1198 Farm Irrigation
- 【排列&字典序】Next Permutation
- Windows核心编程——》第二十章 DLL高级技巧 (DLL Advanced Techniques)
- 修改本地仓库地址
- hdu 1588 Gauss Fibonacci(函数嵌套、转换、等比矩阵求和)
- 如何连接宏参数
- C/C++知识点总结(五)
- 关于服务器内存的选择
- jquery使用技巧(2)
- Android 保存图片到SQLite,读出SQLite中的图片