-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathnext_permutation.cpp
More file actions
44 lines (40 loc) · 1.11 KB
/
next_permutation.cpp
File metadata and controls
44 lines (40 loc) · 1.11 KB
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
class Solution {
public:
// 原理分析
// 以162543为例,下一个应该是163245
// 步骤如下:
// 1. 找到最后一个num[i] < num[i + 1]的点。
// 2. 如果第一步没找到,说明已经是最后一个排练,直接逆序返回即可。
// 3. 从num[i + 1]到num[size - 1]找到大于num[i]的最小的数,并与num[i]交换位置。
// 4. 对num[i + 1]到num[size - 1]排序。
void nextPermutation(vector<int> &num) {
int size =num.size();
int first_index = -1;
int second_index = -1;
int min = INT_MAX;
int i;
if (size <= 1) {
return;
}
for (i = 0; i < (size - 1); ++i) {
if (num[i] < num[i + 1]) {
first_index = i;
}
}
if (-1 == first_index) {
reverse(num.begin(), num.end());
}
else {
for (i = first_index + 1; i < size; ++i) {
if (num[i] > num[first_index]) {
if (num[i] < min) {
min = num[i];
second_index = i;
}
}
}
swap(num[first_index], num[second_index]);
sort(num.begin() + first_index + 1, num.end());
}
}
};