题目:
代码(首刷自解 2024年2月6日):
class Solution {
private:
vector<vector<int>> res;
vector<int> path;
public:
void backtracing(vector<int>& nums, vector<bool>& used1, vector<bool>& used2) {
if (path.size() == nums.size()) {
res.push_back(path);
return;
}
for (int i = 0; i < nums.size(); ++i) {
if (i > 0 && nums[i] == nums[i - 1] && used1[i - 1] == true) continue;
if (used2[i] == true) continue;
used1[i] = true;
used2[i] = true;
path.push_back(nums[i]);
backtracing(nums, used1, used2);
path.pop_back();
used2[i] = false;
used1[i] = false;
}
return;
}
vector<vector<int>> permuteUnique(vector<int>& nums) {
vector<bool> used1(nums.size(), 0);
vector<bool> used2(used1);
sort(nums.begin(), nums.end());
backtracing(nums, used1, used2);
return res;
}
};