题干:
代码:
class Solution {
public:vector<int> tmp;vector<vector<int>> res;void backtracking(vector<int> nums, vector<int> used){if(tmp.size() == nums.size()){res.push_back(tmp);return;}sort(nums.begin(), nums.end());for(int i = 0; i < nums.size(); i++){if(i > 0 && nums[i] == nums[i - 1] && used[i - 1] == 0){continue;//树层去重}if(used[i] == 1)continue;//树枝去重tmp.push_back(nums[i]);used[i] = 1;backtracking(nums, used);tmp.pop_back();used[i] = 0;}}vector<vector<int>> permuteUnique(vector<int>& nums) {vector<int> used(nums.size(), 0);backtracking(nums, used);return res;}
};
唯一区别就是多了两行树层去重代码,而且代码与前面组合问题的树层去重代码一致。