-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path46.permutations.cc
More file actions
57 lines (50 loc) · 1.21 KB
/
46.permutations.cc
File metadata and controls
57 lines (50 loc) · 1.21 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
57
#include <algorithm>
#include <vector>
#include "leetcode_common_struct.h"
using std::vector;
// Runtime: 5 ms, faster than 56.71% of C++ online submissions for Permutations.
// Memory Usage: 7.5 MB, less than 91.87% of C++ online submissions for
// Permutations.
vector<vector<int>> permute(vector<int> &nums) {
vector<vector<int>> res;
std::sort(nums.begin(), nums.end());
do {
res.push_back(nums);
int i = nums.size() - 1;
for (; i > 0; --i) {
if (nums[i - 1] < nums[i]) {
int j = nums.size() - 1;
for (; j >= i; --j) {
if (nums[j] > nums[i - 1]) {
break;
}
}
std::swap(nums[i - 1], nums[j]);
for (; j > i; --j) {
if (nums[j] < nums[j - 1]) {
break;
}
std::swap(nums[j - 1], nums[j]);
}
break;
}
}
int j = nums.size() - 1;
while (i < j) {
std::swap(nums[i], nums[j]);
++i;
--j;
}
} while (nums != res[0]);
return res;
}
int main(int argc, char **argv) {
vector<int> nums;
for (auto i = 1; i < argc; ++i) {
nums.push_back(std::atoi(argv[i]));
}
auto res = permute(nums);
for (const auto &v : res) {
PrintVect(v);
}
}