forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0031.cpp
More file actions
30 lines (29 loc) · 680 Bytes
/
0031.cpp
File metadata and controls
30 lines (29 loc) · 680 Bytes
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
29
30
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
static int x = [](){std::ios::sync_with_stdio(false);cin.tie(0);return 0;}();
class Solution
{
public:
void nextPermutation(vector<int>& nums)
{
int i = nums.size()-1, j = i;
while (i > 0 and nums[i] <= nums[i-1]) --i;
--i;
if (i >= 0)
{
while (j >= i and nums[j] <= nums[i]) --j;
int tmp = nums[i];
nums[i] = nums[j]; nums[j] = tmp;
}
++i;
reverse(nums.begin()+i, nums.end());
}
};
int main()
{
vector<int> arr = { 3, 2, 2, 3};
Solution().nextPermutation(arr);
return 0;
}