题解 | #有重复项数字的全排列#
有重复项数字的全排列
https://www.nowcoder.com/practice/a43a2b986ef34843ac4fdd9159b69863
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param num int整型vector * @return int整型vector<vector<>> */ vector<vector<int>> res; void pailie(vector<int>& num, int n, int k, vector<int>& path, vector<bool>& mark) { if (k == n) { res.push_back(path); return; } for (int i = 0; i < n; i++) { if (!mark[i]) { path.push_back(num[i]); mark[i] = true; pailie(num, n, k + 1, path, mark); path.pop_back(); mark[i] = false; } } } vector<vector<int> > permuteUnique(vector<int>& num) { vector<bool> mark(num.size(), false); vector<int> path; pailie(num, num.size(), 0, path, mark); sort(res.begin(),res.end()); res.erase(unique(res.begin(),res.end()),res.end()); return res; } };