forked from daiwb/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnextPermutation.cpp
More file actions
56 lines (47 loc) · 1.31 KB
/
nextPermutation.cpp
File metadata and controls
56 lines (47 loc) · 1.31 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
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define RFOR(i,a,b) for(int i=(a);i>=(b);--i)
class Solution {
public:
void nextPermutation(vector<int> &num) {
int n = num.size();
int idx = n - 1;
while (idx - 1 >= 0 && num[idx - 1] >= num[idx]) --idx;
if (--idx <= -1) {
reverse(num.begin(), num.end());
return;
}
int lt = idx + 1, rt = n - 1;
while (lt <= rt) {
int mt = (lt + rt) / 2;
if (num[mt] <= num[idx]) {
rt = mt - 1;
} else {
lt = mt + 1;
}
}
swap(num[idx], num[rt]);
reverse(num.begin() + idx + 1, num.end());
}
};
void test(int mm[], int len) {
vector<int> num;
num.assign(mm, mm + len);
Solution s;
s.nextPermutation(num);
REP(i,num.size()) cout << num[i] << " ";
cout << endl;
}
int main() {
int mm1[3] = {1, 2, 3};
test(mm1, sizeof(mm1) / sizeof(mm1[0]));
int mm2[] = {3, 2, 1};
test(mm2, sizeof(mm2) / sizeof(mm2[0]));
int mm3[] = {1, 1, 5};
test(mm3, sizeof(mm3) / sizeof(mm3[0]));
return 0;
}