Use unordered_set to record if a certain number has been used.
class Solution {
public:
vector<vector<int> > permuteUnique(vector<int> &num) {
vector<vector<int> > r;
doPermute(num, 0, r);
return r;
}
void doPermute(vector<int> &num, int s, vector<vector<int> > &r) {
if (s == num.size()) {
r.push_back(num);
return;
}
unordered_set<int> used;
for (int i=s; i<num.size(); i++) {
if (used.find(num[i]) == used.end()) {
swap(num[s], num[i]);
doPermute(num, s+1, r);
swap(num[s], num[i]);
used.insert(num[i]);
}
}
}
};