-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path46.cpp
More file actions
executable file
·43 lines (43 loc) · 1.13 KB
/
46.cpp
File metadata and controls
executable file
·43 lines (43 loc) · 1.13 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
#include <vector>
using namespace std;
class Solution {
public:
vector<int> tmpnums;
vector<int> original;
vector<int> tmp;
vector<vector<int> > answer;
vector<vector<int> > realanswer;
void dfs(int layer){
if (layer == tmpnums.size())
{
answer.push_back(tmp);
return;
}
for (int i = 1; i < tmpnums.size()+1; i++){
bool flag = false;
for (int j = 0; j < layer;j++)
if (tmp[j] == i){
flag = true;
break;
}
if (flag == false){
tmp.push_back(i);
dfs(layer+1);
tmp.pop_back();
}
}
}
vector<vector<int> > permute(vector<int>& nums) {
tmpnums = nums;
original = nums;
dfs(0);
for (int i = 0; i < answer.size();i++){
vector<int> tmp;
for (int j = 0; j < answer[i].size();j++){
tmp.push_back(original[answer[i][j]-1]);
}
realanswer.push_back(tmp);
}
return realanswer;
}
};