class Solution {
public:
vector<bool> used;
vector<vector<int>> res;
void helper(vector<int>& nums, int index, vector<int> curres){
if (index == nums.size()) res.push_back(curres);
else{
for (int i=0; i<nums.size();){
int nxti = i+1;
if (!used[i]){
while(nxti < nums.size() && nums[nxti] == nums[nxti-1]) nxti++;
curres.push_back(nums[i]);
used[i] = true;
helper(nums, index+1, curres);
curres.pop_back();
used[i] = false;
}
i = nxti;
}
}
return;
}
vector<vector<int>> permuteUnique(vector<int>& nums) {
used = vector<bool>(nums.size(), false);
sort(nums.begin(), nums.end());
helper(nums, 0, vector<int>());
return res;
}
};