-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPermutations.cpp
More file actions
35 lines (35 loc) · 918 Bytes
/
Permutations.cpp
File metadata and controls
35 lines (35 loc) · 918 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
class Solution {
private:
vector<vector<int> > ret;
public:
void dfs(vector<int> &num,vector<bool> &used,int dep,int maxdep,vector<int> path)
{
if(dep==maxdep)
{
ret.push_back(path);
return;
}
for(int i=0;i<num.size();i++)
{
if(!used[i])
{
used[i]=true;
path.push_back(num[i]);
dfs(num,used,dep+1,maxdep,path);
used[i]=false;
path.pop_back();
}
}
}
vector<vector<int> > permute(vector<int> &num) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
ret.clear();
if(num.size()==0) return ret;
vector<bool> used(num.size(),false);
vector<int> path;
path.clear();
dfs(num,used,0,num.size(),path);
return ret;
}
};