1053. 交换一次的先前排列

leetcode-1053. 交换一次的先前排列

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
public:
vector<int> prevPermOpt1(vector<int>& arr) {
int n = arr.size() - 1;
while(arr[n] >= arr[n-1])
{
n--;
if(n == 0)
return arr;
}
int temp = n - 1;
n = arr.size() - 1;
while(arr[temp] <= arr[n])
{
n--;
}
while(arr[n - 1] == arr[n])
{
n--;
}
swap(arr[n], arr[temp]);
return arr;
}
};