forked from gh877916059/LintCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path051. 上一个排列.cpp
More file actions
29 lines (29 loc) · 801 Bytes
/
051. 上一个排列.cpp
File metadata and controls
29 lines (29 loc) · 801 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
class Solution
{
public:
vector<int> previousPermuation(vector<int> &nums)
{
int n = nums.size();
vector<int> temp;
for(int i=n-1;i>=0;--i)
{
for(int j=n-1;j>i;--j)
{
if(nums[i]>nums[j])
{
swap(nums[i],nums[j]);
for(int k=i+1;k<n;++k)
temp.push_back(nums[k]);
sort(temp.begin(),temp.end());
reverse(temp.begin(),temp.end());
for(int k=i+1;k<n;++k)
nums[k]=temp[k-i-1];
return nums;
}
}
}
sort(nums.begin(),nums.end());
reverse(nums.begin(),nums.end());
return nums;
}
};