标签:
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]
.
class Solution { public: bool isSwap(vector<int> &num, int i, int j) { int ii=i; while(num[ii]!=num[j]&&ii<j)ii++; if (ii==j)return true; else return false; } void perm(vector<int> &num, int i, int N, vector<vector<int>> &res) { if(i==N) res.push_back(num); for (int j=i;j<N;j++) { if (!isSwap(num,i,j))continue; swap(num[i],num[j]); perm(num,i+1,N,res); swap(num[j],num[i]); } } vector<vector<int> > permuteUnique(vector<int> &num) { vector<vector<int> > res; sort(num.begin(),num.end()); perm(num,0,num.size(),res); return res; } };
标签:
原文地址:http://www.cnblogs.com/Vae98Scilence/p/4283563.html