-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpermutations.cpp
More file actions
56 lines (45 loc) · 1.06 KB
/
Copy pathpermutations.cpp
File metadata and controls
56 lines (45 loc) · 1.06 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
/*
Permutations
Given a collection of numbers, return all possible permutations.
For example,
[1,2,3] have the following permutations:
[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1].
*/
#include <iostream>
#include <vector>
using namespace std;
class Solution
{
public:
void dfs(vector<int> &num, vector<vector<int>> &res, int index,
int n)
{
if (index == n) {
res.push_back(num);
} else {
for (int i = index; i < n; i++) {
swap(num[index], num[i]);
dfs(num, res, index + 1, n);
swap(num[index], num[i]);
}
}
}
vector<vector<int> > permute(vector<int> &num)
{
vector<vector<int>> ret;
dfs(num, ret, 0, num.size());
return ret;
}
};
int main(int argc, char* argv[])
{
vector<int> v = { 1, 2, 3 };
Solution sol;
vector<vector<int>> res = sol.permute(v);
for (auto &v1 : res) {
for (auto t : v1)
cout << t << ends;
cout << endl;
}
return 0;
}