-
Notifications
You must be signed in to change notification settings - Fork 0
/
191120-1.cpp
96 lines (93 loc) · 1.72 KB
/
191120-1.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// https://leetcode-cn.com/problems/next-permutation/
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
void nextPermutation(vector<int>& nums) {
process(&nums[0], nums.size());
}
private:
void process(int* a, int n) {
if (n < 2) return;
if (n == 2) {
swap(a[0], a[1]);
} else if (!isLargest(a + 1, n - 1)) {
process(a + 1, n - 1);
} else if (a[0] >= a[1]) {
for (int i = 0, j = n - 1; i < j; ++i, --j) {
swap(a[i], a[j]);
}
} else {
for (int i = n - 1; i > 0; --i) {
if (a[0] < a[i]) {
swap(a[0], a[i]);
break;
}
}
sort(a + 1, a + n);
}
}
bool isLargest(const int* a, int n) {
if (n < 2) {
return true;
} else if (n == 2) {
return (a[0] >= a[1]);
} else {
return (a[0] >= a[1]) && isLargest(a + 1, n - 1);
}
}
};
void print(const vector<int>& a)
{
for (const auto& e : a) {
printf("%d ", e);
}
printf("\n");
}
int main()
{
Solution s;
{
vector<int> a = { 1, 2, 3 };
s.nextPermutation(a);
print(a); // answer: 1, 3, 2
}
{
vector<int> a = { 3, 2, 1 };
s.nextPermutation(a);
print(a); // answer: 1, 2, 3
}
{
vector<int> a = { 1, 1, 5 };
s.nextPermutation(a);
print(a); // answer: 1, 5, 1
}
{
vector<int> a = { 1, 2, 3, 4 };
s.nextPermutation(a);
print(a); // answer: 1, 2, 4, 3
}
{
vector<int> a = { 1, 1, 1, 1 };
s.nextPermutation(a);
print(a); // answer: 1, 1, 1, 1
}
{
vector<int> a = { 1, 1, 5, 4 };
s.nextPermutation(a);
print(a); // answer: 1, 4, 1, 5
}
{
vector<int> a = { 3, 2, 1, 1 };
s.nextPermutation(a);
print(a); // answer: 1, 1, 2, 3
}
{
vector<int> a = { 3, 4, 1, 1 };
s.nextPermutation(a);
print(a); // answer: 4, 1, 1, 3
}
return 0;
}