- tags: LeetCode,backtracking
视频解析:https://www.youtube.com/watch?v=s7AvT7cGdSo
在 LeetCode: 46. Permutations 的基础上增加重复的元素。感觉不能依赖于 track
+ map
的去重逻辑回溯。
class Solution {
public:
vector<vector<int>> permuteUnique(vector<int>& nums) {
vector<vector<int>> res;
}
};
数据特征:
Value: 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1,
Index: 2, 1, 0, 1, 0, 2, 1, 0, 1, 0, 2, 1, 0, 1, 0,
class Solution {
public:
vector<vector<int>> permuteUnique(vector<int>& nums) {
set<vector<int>> res;
vector<vector<int>> ret;
int n, i;
vector<vector<int>> perms;
if (nums.size() == 1) {
ret.push_back(nums);
return ret;
}
for (i = 0; i < nums.size(); i++) {
n = nums.back();
nums.pop_back();
perms = permuteUnique(nums);
for (auto perm : perms) {
perm.push_back(n);
res.insert(perm);
}
nums.insert(nums.begin(), n);
}
for (auto r : res) {
ret.push_back(r);
}
return ret;
}
};