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
思路:下一个排列算法流程如下,注意降序的话直接翻转。
class Solution {
public:
void nextPermutation(vector<int>& nums) {
int k=-1,n=nums.size();
for(int i=n-2;i>=0;i--){
if(nums[i]<nums[i+1]){
k=i;
break;
}
}
if(k==-1){
reverse(nums.begin(),nums.end());
return;
}
int l=-1;
for(int i=n-1;i>k;i--){
if(nums[i]>nums[k]){
l=i;
break;
}
}
swap(nums[k],nums[l]);
reverse(nums.begin()+k+1,nums.end());
}
};