Given a list of numbers with duplicate number in it. Find all unique permutations.
可以见我的博文全排列实现
class Solution {
public:
/**
* @param nums: A list of integers.
* @return: A list of unique permutations.
*/
vector<vector<int> >res;
void dfs(vector<int> &nums,vector<int> &sub,int cur){
if(cur==nums.size()){
res.push_back(sub);
return;
}
for(int i=0;i<nums.size();i++){
if(i==0||nums[i]!=nums[i-1]){
int c1=0,c2=0;
for(int j=0;j<nums.size();j++){
if(nums[j]==nums[i]){
c1++;
}
}
for(int j=0;j<cur;j++){
if(sub[j]==nums[i]){
c2++;
}
}
if(c2<c1){
sub[cur]=nums[i];
dfs(nums,sub,cur+1);
}
}
}
}
vector<vector<int> > permuteUnique(vector<int> &nums) {
// write your code here
int len=nums.size();
if(len==0){
return res;
}
sort(nums.begin(),nums.end());
vector<int> sub(len);
dfs(nums,sub,0);
return res;
}
};