代码
class Solution {
public:
void nextPermutation(vector<int> &num) {
int length = num.size()-1;
while(length>0)
{
if(num[length-1] < num[length])
break;
--length;
}
if(length == 0)
{
reverse(num.begin(), num.end());
return ;
}
int k = num.size() - 1;
for(; k>=length; --k)
{
if(num[k]>num[length-1])
break;
}
swap(num[length-1], num[k]);
sort(num.begin()+length, num.end());
return ;
}
};