leetcode: Next Permutation
来源:互联网 发布:删除cadbak文件软件 编辑:程序博客网 时间:2024/06/05 19:28
Given a list of integers, which denote a permutation.
Find the next permutation in ascending order.
Notice
The list may contains duplicate integers.
Example
For [1,3,2,3]
, the next permutation is [1,3,3,2]
For [4,3,2,1]
, the next permutation is [1,2,3,4]
class Solution { private: void swap(vector<int> &num, int left, int right) { int tmp = num[left]; num[left] = num[right]; num[right] = tmp; } public: /** * @param nums: An array of integers * @return: An array of integers that's next permuation */ vector<int> nextPermutation(vector<int> &nums) { // write your code here vector<int> retVtr = nums; int pivotIdx = retVtr.size()-1; for (; pivotIdx>0; pivotIdx--) { if (retVtr[pivotIdx-1] < retVtr[pivotIdx]) break; } if (pivotIdx > 0) { int left = pivotIdx-1; int right = pivotIdx; for (int i=pivotIdx; i<retVtr.size(); i++) { if (retVtr[i] > retVtr[left]) { right = i; } } swap(retVtr, left, right); } sort(retVtr.begin()+pivotIdx, retVtr.end()); return retVtr; }};
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
- java--自定义标签(tag、tld两种)
- mybatis第2天
- POJ 3678 Katu Puzzle 2-SAT
- JVM(一)--概述
- 按订单号(String常量池唯一,即对象唯一)加同步锁
- leetcode: Next Permutation
- 我的Python学习之路之基本语法-输入输出
- Python2.6和Python3.0中对除法操作的异同
- async
- 通过label标记实现单选框点击文字也能选中
- linux,ubuntu下软件的更新命令
- Python 技巧(三)—— list 删除一个元素的三种做法
- smarty模板引擎_4-内置的变量调节器
- gSoftware vs gnome-packagekit