【answer】
From right to left to find the position of a first drop I (i.e. satisfies nums [i] <nums [i + 1]);
then [i + 1..len-1] found inside this interval a maximum subscript k , so that the nums [K]> the nums I
note that the nature of a (i + 1..len-1) this paragraph is a monotonically decreasing
and swap (nums [k], nums [i]);
and then [i + 1 ..len-1] this stretch of flipping it.
You can get a next_permutation
[Code]
class Solution {
public:
void swap(int &a,int &b){
int t = a;a = b;b = t;
}
void _reverse(vector<int>&nums,int l,int r){
while(l<=r){
swap(nums[l],nums[r]);
l++;r--;
}
}
void nextPermutation(vector<int>& nums) {
int len = nums.size();
int j = -1;
for (int i = len-1;i >= 0;i--){
if (i-1>=0 && nums[i]>nums[i-1]){
j = i-1;
break;
}
}
if (j==-1){
_reverse(nums,0,len-1);
}else{
for (int i = len-1;i >= 0;i--){
if (nums[i]>nums[j]){
swap(nums[i],nums[j]);
break;
}
}
_reverse(nums,j+1,len-1);
}
}
};