-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathPrevious_Permuation.cpp
53 lines (47 loc) · 1.21 KB
/
Previous_Permuation.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
Given a list of integers, which denote a permutation.
Find the previous permutation in ascending order.
Note
The list may contains duplicate integers.
Example
For [1,3,2,3], the previous permutation is [1,2,3,3]
For [1,2,3,4], the previous permutation is [4,3,2,1]
*/
#include <vector>
using namespace std;
class Solution {
public:
/**
* @param nums: An array of integers
* @return: An array of integers that's previous permuation
*/
vector<int> previousPermuation(vector<int>& nums) {
// write your code here
int index = nums.size() - 1;
while (index > 0) {
if (nums[index - 1] > nums[index]) {
break;
}
index--;
}
if (index > 0) {
int i = index - 1;
int j = nums.size() - 1;
while (j > i) {
if (nums[j] < nums[i]) {
break;
}
j--;
}
swap(nums[i], nums[j]);
}
int begin = index;
int end = nums.size() - 1;
while (begin < end) {
swap(nums[begin], nums[end]);
begin++;
end--;
}
return nums;
}
};