题解 | #没有重复项数字的全排列#
没有重复项数字的全排列
https://www.nowcoder.com/practice/4bcf3081067a4d028f95acee3ddcd2b1
class Solution {
public:
vector<vector<int>> res;
vector<vector<int> > permute(vector<int> &num) {
vector<int> a;
vector<bool> flag(num.size(),true);
for(int i=0;i<num.size();i++){
a.push_back(num[i]);
flag[i]=false;
dfs(num,a,flag);
a.pop_back();
flag[i]=true;
}
return res;
}
void dfs(vector<int> &num,vector<int> a,vector<bool> &flag){
if(a.size()==num.size()){
res.push_back(a);
}
for(int i=0;i<num.size();i++){
if(flag[i]==false) continue;
a.push_back(num[i]);
flag[i]=false;
dfs(num, a,flag);
a.pop_back();
flag[i]=true;
}
}
};