题解 | #没有重复项数字的全排列#
没有重复项数字的全排列
http://www.nowcoder.com/practice/4bcf3081067a4d028f95acee3ddcd2b1
class Solution {
public:
int total = 0;
vector<vector<int> > ans;
vector<int> temp;
map<int,int> mp;
void dfs(vector<int> &num,int n,int cnt){
if(cnt == n){
ans.push_back(temp);
return ;
}
for(int i = 0;i < n;++i){
if(!mp[num[i]]){
mp[num[i]] = 1;
temp.push_back(num[i]);
dfs(num,n,cnt + 1);
temp.pop_back();
mp[num[i]] = 0;
}
}
}
vector<vector<int> > permute(vector<int> &num) {
for(int i = 0;i < num.size();++i)
mp[num[i]] = 0;
dfs(num,num.size(),0);
return ans;
}
};