-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPermutations.cpp
More file actions
40 lines (38 loc) · 952 Bytes
/
Permutations.cpp
File metadata and controls
40 lines (38 loc) · 952 Bytes
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
/***
* DFS
* 用一个数组标记某个数是否被使用
* 如果没被使用,则可以使用
* 时间复杂度O(n!) 空间复杂度O(n)
***/
class Solution {
public:
vector<vector<int> > permute(vector<int> &num) {
vused.resize(num.size(), false);
vector<int> path;
dfs(num, path);
return vvpers;
}
private:
vector<vector<int> > vvpers;
vector<bool> vused;
void dfs(vector<int> &num, vector<int> &path)
{
if (path.size() == num.size()) // the end
{
vvpers.push_back(path);
return;
}
for (int i = 0; i < num.size(); ++i)
{
if (!vused[i]) // available
{
vused[i] = true;
path.push_back(num[i]);
dfs(num, path); // next deep
path.pop_back(); // back tracking
vused[i] = false;
}
}
return;
}
};