forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 14
/
next-permutation.cpp
67 lines (59 loc) · 1.75 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Time: O(n)
// Space: O(1)
class Solution {
public:
void nextPermutation(vector<int>& nums) {
int pivot = -1;
for (int i = nums.size() - 2; i >= 0; --i) {
if (nums[i] < nums[i + 1]) {
pivot = i;
break;
}
}
if (pivot == -1) {
reverse(begin(nums), end(nums));
return;
}
for (int i = nums.size() - 1; i > pivot; --i) {
if (nums[i] > nums[pivot]) {
swap(nums[pivot], nums[i]);
break;
}
}
reverse(begin(nums) + pivot + 1, end(nums));
}
};
class Solution2 {
public:
void nextPermutation(vector<int> &num) {
nextPermutation(num.begin(), num.end());
}
private:
template<typename BidiIt>
bool nextPermutation(BidiIt begin, BidiIt end) {
const auto rbegin = reverse_iterator<BidiIt>(end);
const auto rend = reverse_iterator<BidiIt>(begin);
// Find the first element (pivot) which is less than its successor.
auto pivot = next(rbegin);
while (pivot != rend && *pivot >= *prev(pivot)) {
++pivot;
}
bool is_greater = true;
if (pivot != rend) {
// Find the number which is greater than pivot, and swap it with pivot
auto change = find_if(rbegin, pivot, bind1st(less<int>(), *pivot));
swap(*change, *pivot);
} else {
is_greater = false;
}
// Make the sequence after pivot non-descending
reverse(rbegin, pivot);
return is_greater;
}
};
class Solution3 {
public:
void nextPermutation(vector<int> &num) {
next_permutation(num.begin(), num.end());
}
};