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:
vector<vector<int> > permute(vector<int> &num) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
set<vector<int> >ret;
int n=num.size();
_permute(num,n,n-1,ret);
return vector<vector<int> >(ret.begin(),ret.end());
}
void _permute(vector<int>& array,int n,int depth,vector<vector<int> >&ret){
if(depth==0){
ret.insert(array);
return;
}
for(int i=0 ; i<=depth ; i++){
swap(array[i],array[depth]);
_permute(array,n,depth-1,ret);
swap(array[i],array[depth]);
}
return;
}
};
本文探讨了如何处理包含重复数字的集合,并找到所有可能的独特排列组合。通过实例展示了解决这类问题的算法步骤,包括使用集合来去除重复元素并递归地生成排列。

被折叠的 条评论
为什么被折叠?



