CODE 102: Next Permutation
来源:互联网 发布:沉默的羔羊解析知乎 编辑:程序博客网 时间:2024/04/29 20:34
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 void nextPermutation(int[] num) {// IMPORTANT: Please reset any member data you declared, as// the same Solution instance will be reused for each test case.List<Integer> rest = new ArrayList<Integer>();int i = 0;for (i = num.length - 1; i >= 1; i--) {if (num[i] <= num[i - 1]) {rest.add(num[i]);} else {break;}}if (i <= 0) {Arrays.sort(num);} else {int j = 0;rest.add(num[i]);for (; j < rest.size(); j++) {if (rest.get(j) > num[i - 1]) {break;}}int tmp = rest.get(j);rest.set(j, num[i - 1]);num[i - 1] = tmp;for (j = 0; i < num.length; i++, j++) {num[i] = rest.get(j);}}}
- CODE 102: Next Permutation
- [leet code] Next Permutation
- leetcode 日经贴,python code -Next Permutation
- 【Leet Code】31. Next Permutation---Medium
- Next Permutation
- Next Permutation
- Next Permutation
- Next Permutation
- Next Permutation
- Next Permutation
- Next Permutation
- Next Permutation
- Next Permutation
- Next Permutation
- Next Permutation
- Next Permutation
- Next Permutation
- Next Permutation
- 源中的 backports proposed security updates 什么意思?
- 开阔自己的视野,勇敢的接触新知识
- hash总结
- php文件上传出现的小问题
- HDU 3336 KMP 求所有前缀在母串中出现的次数
- CODE 102: Next Permutation
- VPN原理基础
- HDFS读取文件操作的处理流程源码分析
- hdu-2107-Founding of HDU
- 应用程序基础之02-应用程序组件篇(二)
- VC2012下MFC程序各控件的常用方法
- Oracle 子查询
- Android学习篇章19-动画:Tween动画
- 什么是模逆元素?