题解 | #有重复项数字的全排列#
有重复项数字的全排列
http://www.nowcoder.com/practice/a43a2b986ef34843ac4fdd9159b69863
class Solution { public: vector<vector<int> > permuteUnique(vector<int> &num) { vector<vector<int>> res; vector<int> path; vector<bool> used(num.size(), false); sort(num.begin(), num.end()); dfs(num, res, path, used); return res; } void dfs(vector<int>& num, vector<vector<int>>& res, vector<int>& path, vector<bool>& used){ if(path.size()==num.size()){ res.push_back(path); return; } for(int i=0; i<num.size(); i++){ if(i>0 && num[i-1]==num[i] && used[i-1]==true){ continue; } if(used[i]==false){ path.push_back(num[i]); used[i] = true; dfs(num, res, path, used); used[i] = false; path.pop_back(); } } } };
https://www.programmercarl.com/0047.%E5%85%A8%E6%8E%92%E5%88%97II.html#%E6%80%9D%E8%B7%AF