forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_31.cpp
33 lines (29 loc) · 769 Bytes
/
_31.cpp
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
33
class Solution {
public:
void nextPermutation(vector<int>& nums) {
int i1;
int i2;
bool hasPermutation = false;
for(int i=nums.size()-1; i>0; i--){
if(nums[i-1]<nums[i]){
i1 = i-1;
i2 = i;
hasPermutation = true;
break;
}
}
if(hasPermutation){
int j=i2;
for(int i=nums.size()-1; i>i1; i--){
if(nums[i]>nums[i1]){
j=i;
break;
}
}
swap(nums[i1], nums[j]);
reverse(nums.begin()+i1+1, nums.end());
}else{
sort(nums.begin(), nums.end());
}
}
};