-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path31.cpp
More file actions
32 lines (30 loc) · 746 Bytes
/
Copy path31.cpp
File metadata and controls
32 lines (30 loc) · 746 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
31
32
class Solution {
public:
void nextPermutation(vector<int>& nums) {
int len = nums.size();
if(len == 0)return;
bool flag = false;
for(int i = len-1; i > 0; --i)
{
if(nums[i] > nums[i-1])
{
int j;
for(j = len-1; j > i-1; --j)
{
if(nums[j] > nums[i-1])break;
}
swap(nums[i-1], nums[j]);
sort(nums.begin()+i, nums.end());
flag = true;
break;
}
}
if(!flag)
{
for(int i = 0; i <= len/2-1; i++)
{
swap(nums[i], nums[len-1-i]);
}
}
}
};