-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNext_Permutation.cpp
45 lines (39 loc) · 1.41 KB
/
Next_Permutation.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
// Implement next permutation, which rearranges numbers into
// the lexicographically next greater permutation of numbers.
// If such arrangement is not possible, it must rearrange it
// as the lowest possible order (ie, sorted in ascending order).
// The replacement must be in-place, do not allocate extra memory.
// Here are some examples. Inputs are in the left-hand column
// and its corresponding outputs are in the right-hand column.
// 1,2,3 → 1,3,2
// 3,2,1 → 1,2,3
// 1,1,5 → 1,5,1
class Solution {
public:
void nextPermutation(vector<int> &num) {
// record the index of last digit pair of ascending order.
int index = -1;
for (int i = num.size()-1; i > 0; i--) {
if (num[i] > num[i-1]) {
index = i;
break;
}
}
if (index == -1) {
sort(num.begin(), num.end());
return;
}
// find the next greater digit of num[index-1] in 'num' since index
// swap them, then reorder the sub-array of 'num' since num[index].
int nextBigger = num[index];
int idx = index;
for (int i = index+1; i < num.size(); i++) {
if (num[i] > num[index-1] && nextBigger > num[i]) {
nextBigger = num[i];
idx = i;
}
}
swap(num[idx], num[index-1]);
sort(num.begin()+index, num.end());
}
};