题解 | #有重复项数字的所有排列#
有重复项数字的所有排列
http://www.nowcoder.com/practice/a43a2b986ef34843ac4fdd9159b69863
递归+交换
class Solution {
public:
void permute(vector<int> num,int t,vector<vector<int> > &res){
if(num.size()==t){
res.push_back(num);
return;
}
permute(num,t+1,res);
for(int i=t;i<num.size();i++){
if(num[t]!=num[i]){
swap(num[t],num[i]);
permute(num,t+1,res);
}
}
return;
}
vector<vector<int> > permuteUnique(vector<int> &num) {
vector<vector<int> > res;
if(num.size()<=1){
res.push_back(num);
return res;
}
sort(num.begin(),num.end());
permute(num,0,res);
return res;
}
};