[LeetCode]Next Permutation
来源:互联网 发布:linux find 绝对路径 编辑:程序博客网 时间:2024/05/21 17:53
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 nextPermutation(int[] num) {int len = num.length;int k = 0;for (int i = len - 1; i > 0; i--) {if (num[i] > num[i - 1]) {k = i;break;}}if (k == 0) {Arrays.sort(num);return;}for (int i = len - 1; i >= k; i--) {if (num[k - 1] < num[i]) {num[k - 1] = num[k - 1] ^ num[i];num[i] = num[k - 1] ^ num[i];num[k - 1] = num[k - 1] ^ num[i];break;}}Arrays.sort(num, k, len);return;}}
0 0
- LeetCode: Next Permutation
- LeetCode Next Permutation
- LeetCode : Next Permutation
- [Leetcode] Next Permutation
- [LeetCode] Next Permutation
- [Leetcode] Next Permutation
- leetcode 88: Next Permutation
- [LeetCode]Next Permutation
- LeetCode-Next Permutation
- [leetcode]Next Permutation
- LeetCode - Next Permutation
- LeetCode 31: Next Permutation
- 【leetcode】Next Permutation
- LeetCode题解:Next Permutation
- 【LeetCode】Next Permutation
- [LeetCode] Next Permutation
- LeetCode Next Permutation
- leetCode | Next Permutation
- OpengL学习之光照和材质小窥
- Jlink v7改进版升级v8必备文章(小修复~)
- Iterator与iterable的区别
- Hibernate中HQL和QBC
- Comparable与Comparator的区别
- [LeetCode]Next Permutation
- ZigBee学习-初级篇(一)
- 本办法学Python 随记1
- java.lang.ClassNotFoundException错误原因汇总
- 一个google邮箱的秘密
- 2014-9 月个人规划
- Hibernate中hbm.xml文件的inverse、cascade、fetch、outer-join、lazy
- hdu 4995 暴力
- 如何在不插网线的情况下启用网卡的TCP/IP协议