solution.cpp 447 字节
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
	void nextPermutation(vector<int> &nums)
	{
		if (nums.size() < 2)
		{
			return;
		}
		int i = nums.size() - 2;
		while (i >= 0 && nums[i] >= nums[i + 1])
		{
			i--;
		}
		if (i >= 0)
		{
			int j = nums.size() - 1;
			while (j >= 0 && nums[j] >= nums[i])
			{
				j--;
			}
			swap(nums.begin() + i, nums.begin() + j);
		}
		reverse(nums.begin() + i + 1, nums.end());
	}
};