-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0046.permutations.cpp
53 lines (44 loc) · 1.08 KB
/
0046.permutations.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
#include <algorithm>
#include <iostream>
#include <vector>
#include "leetcode.h"
using std::vector;
vector<vector<int>> permute(vector<int>& nums)
{
int n = nums.size();
vector<vector<int>> res;
vector<int> vis(n, 0);
vector<int> path;
if (n == 0) return res;
auto backtracking = [&res, &vis, &nums, n](auto&& self, vector<int>& path) -> void {
if (path.size() == n) {
res.push_back(path);
return ;
}
for (int i = 0; i < n; ++i) {
if (vis[i] == 1) continue;
vis[i] = 1;
path.push_back(nums[i]);
self(self, path);
path.pop_back();
vis[i] = 0;
}
};
backtracking(backtracking, path);
return res;
}
int main () {
#ifdef LOCAL
freopen("0046.in", "r", stdin);
#endif
int n = 0;
while (std::cin >> n) {
vector<int> nums(n, 0);
for (int i = 0; i < n; ++i) {
std::cin >> nums[i];
}
vector<vector<int>> res = permute(nums);
std::cout << res << std::endl;
}
return 0;
}