Given a collection of numbers that might contain duplicates, return all possible unique permutations.
Example:
Input: [1,1,2]
Output:
[
[1,1,2],
[1,2,1],
[2,1,1]
]
class Solution {
public:
vector<vector<int>> permuteUnique(vector<int>& nums) {
vector<int> temp;
vector<vector<int>> res;
sort(nums.begin(),nums.end());
vector<bool> used(nums.size(),false);
backtracking(nums,res,temp,used);
return res;
}
void backtracking(vector<int> & nums,vector<vector<int>> &res,vector<int> &temp,vector<bool> &used){
if(temp.size()==nums.size()){
res.push_back(temp);
}
for(int i=0;i<nums.size();i++){
if(used[i] || i>0 && nums[i-1]==nums[i] && !used[i-1]) continue;
used[i] = true;
temp.push_back(nums[i]);
backtracking(nums,res,temp,used);
used[i] = false;
temp.pop_back();
}
}
};
if(used[i] || i>0 && nums[i-1]==nums[i] && !used[i-1]) continue;/
/前一个used[i]是判断当前是否之前用过,如果不要后面的条件,就会包括[1,1,2],[1,2,1],[1,1,2]…重复的,后面的nums[i-1]==nums[i]来判断是否相同,相同不应该push,后面!used[i-1]是来判断是否是新的一组,即int i=0,i = 1 是否是i的变化