-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path46.cpp
More file actions
85 lines (73 loc) · 1.73 KB
/
46.cpp
File metadata and controls
85 lines (73 loc) · 1.73 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Author : Accagain
// Date : 17/3/26
// Email : chenmaosen0@gmail.com
/***************************************************************************************
*
* Given a collection of distinct 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],
* [3,2,1]]
*
* 做法:
* dfs
*
* 时间复杂度:
*
*
****************************************************************************************/
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <vector>
#include <string>
#define INF 0x3fffffff
using namespace std;
class Solution {
public:
bool vis[12000];
void dfs(vector<int> nums, vector<vector<int>> &ans, int now, vector<int>hav)
{
if(now == nums.size())
{
// for(int i=0; i<hav.size(); i++)
// printf("%d ", hav[i]);
// printf("\n");
ans.push_back(hav);
return ;
}
for(int i=0; i<nums.size(); i++)
{
if(!vis[nums[i]])
{
vis[nums[i]] = 1;
hav.push_back(nums[i]);
dfs(nums, ans, now+1, hav);
hav.pop_back();
vis[nums[i]] = 0;
}
}
}
vector<vector<int>> permute(vector<int>& nums) {
memset(vis, 0, sizeof(vis));
vector<vector<int>> ans;
vector<int>hav;
dfs(nums, ans, 0, hav);
return ans;
}
};
int main() {
Solution *test = new Solution();
int data[] = {1, 2, 3};
vector<int> x(data, data + sizeof(data) / sizeof(data[0]));
test->permute(x);
return 0;
}
//
// Created by cms on 17/3/26.
//