-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path031_NextPermutation.cpp
44 lines (40 loc) · 1.34 KB
/
031_NextPermutation.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
//27.38%
class Solution {
public:
void nextPermutation(vector<int>& nums) {
int size(nums.size());
//Find start number need to be moved forward
int start(size - 1);
while(start > 0 && nums[start - 1] >= nums[start]) start--;
if(start > 0){
int pos(size - 1);
while(nums[pos] <= nums[start - 1])
pos--;
swap(nums[pos], nums[start - 1]);
reverse(nums.begin() + start, nums.end());
}else
reverse(nums.begin(), nums.end());
}
};
//They use a 'upper_bound' here which is interesting
class Solution {
public:
void nextPermutation(vector<int> &num)
{
if (num.empty()) return;
// in reverse order, find the first number which is in increasing trend (we call it violated number here)
int i;
for (i = num.size()-2; i >= 0; --i)
{
if (num[i] < num[i+1]) break;
}
// reverse all the numbers after violated number
reverse(begin(num)+i+1, end(num));
// if violated number not found, because we have reversed the whole array, then we are done!
if (i == -1) return;
// else binary search find the first number larger than the violated number
auto itr = upper_bound(begin(num)+i+1, end(num), num[i]);
// swap them, done!
swap(num[i], *itr);
}
};