-
Notifications
You must be signed in to change notification settings - Fork 0
/
next_permutation.cpp
68 lines (62 loc) · 1.96 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
/*
* =====================================================================================
*
* Filename: next_permutation.cpp
*
* Description: 31. Next Permutation. Implement next permutation, which rearranges
* numbers into the lexicographically next greater permutation of
* numbers. If such arrangement is not possible, it must rearrange it
* as the lowest possible order (ie, sorted in ascending order).
*
* Version: 1.0
* Created: 08/21/18 11:10:00
* Revision: none
* Compiler: gcc
*
* Author: Zhu Xianfeng (), [email protected]
* Organization:
*
* =====================================================================================
*/
#include <algorithm>
#include <vector>
#include "gmock/gmock.h"
#include "gtest/gtest.h"
using std::vector;
/**
* 1. Find the largest index k such that nums[k] < nums[k + 1].
* If no such index exists, just reverse nums and done.
* 2. Find the largest index r > k such that nums[k] < nums[r].
* 3. Swap nums[k] and nums[r].
* 4. Reverse the sub-array nums[k + 1:].
*/
class Solution {
public:
void nextPermutation(vector<int>& nums) {
int k = nums.size() - 2;
while (k >= 0 && nums[k] >= nums[k + 1]) {
k--;
}
if (k < 0) {
std::reverse(nums.begin(), nums.end());
return;
}
int r = nums.size() - 1;
while (r > k && nums[r] <= nums[k]) {
r--;
}
std::swap(nums[k], nums[r]);
std::reverse(nums.begin() + k + 1, nums.end());
}
};
TEST(Solution, nextPermutation) {
vector<std::pair<vector<int>, vector<int>>> cases = {
std::make_pair(vector<int>{1, 2, 3}, vector<int>{1, 3, 2}),
std::make_pair(vector<int>{3, 2, 1}, vector<int>{1, 2, 3}),
std::make_pair(vector<int>{1, 1, 5}, vector<int>{1, 5, 1}),
};
for (auto& c : cases) {
Solution().nextPermutation(c.first);
EXPECT_THAT(c.first, testing::ElementsAreArray(c.second));
}
}