forked from fanfank/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
next-permutation.cpp
75 lines (74 loc) · 2.32 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
68
69
70
71
72
73
74
75
/************************************************
Solution 1: shorter than solution 2
************************************************/
class Solution {
public:
int binarySearchLarge(vector<int> &v, int left, int right, int target) {
while(left < right) {
int middle = left + (right - left + 1) / 2; //get ceiling
if(v[middle] <= target)
right = middle - 1;
else
left = middle;
}
if(v[left] <= target)
return -1;
else
return left;
}
void nextPermutation(vector<int> &num) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
for(int i = num.size() - 2; i >= 0; --i) {
int index = binarySearchLarge(num, i + 1, num.size() - 1, num[i]);
if(index != -1) {
swap(num[index], num[i]);
reverse(num.begin() + i + 1, num.end());
return;
}
}
reverse(num.begin(), num.end());
}
};
/************************************************
Solution 2: another method
************************************************/
class Solution {
public:
int binarySearchLarge(vector<int> &v, int left, int right, int target) {
while(left < right) {
int middle = left + (right - left + 1) / 2; //get ceiling
if(v[middle] <= target)
right = middle - 1;
else
left = middle;
}
if(v[left] <= target)
return -1;
else
return left;
}
bool permute(vector<int> &v, int left, int right) {
if(left >= right) {
return false;
}
if(permute(v, left + 1, right)) {
return true;
} else {
int index = binarySearchLarge(v, left + 1, right, v[left]);
if(index == -1) {
return false;
} else {
swap(v[left], v[index]);
reverse(v.begin() + left + 1, v.end());
return true;
}
}
}
void nextPermutation(vector<int> &num) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(!permute(num, 0, num.size() - 1))
reverse(num.begin(), num.end());
}
};