leetcode-Next Permutation
来源:互联网 发布:ipad1如何下载软件 编辑:程序博客网 时间:2024/06/05 21:11
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
void sorted(int l, int h, vector<int> &num){ for(int i = l; i <(l+h+1)/2; i++) { int temp = num[i]; num[i] = num[l+h-i]; num[l+h-i] = temp; }}class Solution {public: void nextPermutation(vector<int> &num) { int n = num.size(); if(n < 2)return; for(int i = n-1; i > 0; i--) { if(num[i] > num[i-1]) { int j = n-1; while(j > i-1) { if(num[j] > num[i-1]) { int temp = num[j]; num[j] = num[i-1]; num[i-1] = temp; sorted(i, n-1, num); return; } j--; } } } sorted(0, n-1, num); }};
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
- 初识字典树(Trie Tree)
- hdu1426 深搜水题
- A+B prooblem
- 数据在内存中存放的位置
- OpenGL雾(fog)
- leetcode-Next Permutation
- 指针和引用的区别和联系
- python 去除文件中多个换行
- Java栈与堆
- leetcode-Gas Station
- vim之中U命令的浅析
- java synchronized与volatile的区别
- 安卓上onTouchEvent, onClick和onLongClick的调用机制
- 【BZOJ】【P1121】【POI2008】【激光发射器SZK】【题解】【乱搞】