class Solution {
public:
void nextPermutation(vector<int> &num) {
int n = num.size();
if (n == 1)return;
for (int i = n - 2, ii = n - 1; i >= 0; i--, ii--)
if (num[i] < num[ii])
{
int j = n - 1;
while (num[j] <= num[i])j--;//从尾部找到第一个比num[i]大的数,一定可以找到
swap(num[i], num[j]);
reverse(num.begin() + ii, num.end());
return;
}
reverse(num.begin(), num.end());
}
};