Given a collection of numbers that might contain duplicates, return all possible unique permutations.
For example,
[1,1,2]
have the following unique permutations:
[1,1,2]
, [1,2,1]
, and [2,1,1]
.
void visit(vector<int> &num, int n, int pos)
{
if (n == pos)
{
for (int i = 0; i < num.size(); i++)
{
cout << num[i] << " ";
}
cout << endl;
return;
}
for (int i = pos; i < n; i++)
{
if (num[i] == num[pos] && i != pos)
{
continue;
}
swap(num[i], num[pos]);
visit(num, n, pos+1);
swap(num[i], num[pos]);
}
}
void fun(vector<int> &num)
{
int n = num.size();
sort(num.begin(), num.end());
visit(num, n, 0);
}