-
Notifications
You must be signed in to change notification settings - Fork 0
/
191223-1.cpp
51 lines (48 loc) · 864 Bytes
/
191223-1.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
// https://leetcode-cn.com/problems/permutations/
#include <cstdio>
#include <vector>
using namespace std;
class Solution {
public:
vector<vector<int>> permute(vector<int>& nums) {
vector<vector<int>> res;
if (nums.size() <= 1) {
res.push_back(nums);
} else {
int n = nums.size();
for (int i = 0; i < n; ++i) {
int last = nums[i];
vector<int> a = nums;
a.erase(a.begin() + i);
auto sub = permute(a);
for (auto e : sub) {
e.push_back(last);
res.push_back(e);
}
}
}
return res;
}
};
void print(const vector<vector<int>>& a)
{
printf("[\n");
for (auto e : a) {
printf(" [");
for (int i = 0; i < e.size(); ++i) {
if (i > 0) printf(",");
printf("%d", e[i]);
}
printf("]\n");
}
printf("]\n");
}
int main()
{
Solution s;
{
vector<int> a = {1,2,3};
print(s.permute(a));
}
return 0;
}