-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathLeetCode#37.cc
36 lines (32 loc) · 1012 Bytes
/
LeetCode#37.cc
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
class Solution {
private:
void dfs(int ind, vector<int>& num, vector<int>& col, vector<int>& tmp,
vector<vector<int> >& ret){
if(ind==num.size()){
vector<int> tt = tmp;
ret.push_back(tt);
return ;
}
for(int i=0;i<num.size();i++)
if(col[i]==0){
col[i]=1;
tmp[ind]=num[i];
dfs(ind+1,num,col,tmp,ret);
col[i]=0;
}
return ;
}
public:
vector<vector<int> > permute(vector<int> &num) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<int> col(num.size());
vector<int> tmp(num.size());
vector<vector<int> > ret;
if(num.size()==0) return ret;
for(int i=0;i<num.size();i++)
col[i]=0;
dfs(0,num,col,tmp,ret);
return ret;
}
};