题目描述
给出一组可能包含重复项的数字,返回该组数字的所有排列
例如;
[1,1,2]的排列如下:
[1,1,2],[1,2,1], [2,1,1].
[1,1,2],[1,2,1], [2,1,1].
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 {
int array[19]={0};
void permutation(vector<vector<int>> &ans ,vector<int> &num,int k,int n){
if (k==n)
ans.push_back(num);
else
{
for (int i=0;i<19;i++){
if (array[i]>0)
{
array[i]--;
num[k]=i-9;
permutation(ans, num, k+1, n);
array[i]++;
}
}
}
}
public:
vector<vector<int> > permuteUnique(vector<int> &num) {
for (int i=0;i<num.size();i++){
array[num[i]+9]++;
}
vector<vector<int>> ans;
permutation(ans, num, 0, num.size());
return ans;
}
};
int array[19]={0};
void permutation(vector<vector<int>> &ans ,vector<int> &num,int k,int n){
if (k==n)
ans.push_back(num);
else
{
for (int i=0;i<19;i++){
if (array[i]>0)
{
array[i]--;
num[k]=i-9;
permutation(ans, num, k+1, n);
array[i]++;
}
}
}
}
public:
vector<vector<int> > permuteUnique(vector<int> &num) {
for (int i=0;i<num.size();i++){
array[num[i]+9]++;
}
vector<vector<int>> ans;
permutation(ans, num, 0, num.size());
return ans;
}
};