标签:style blog class code java color
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]
.
思路:排列组合基本参照Permutations的思路,但是其中包含重复的排列组合。这就需要考虑在全排列中去掉重复的规则,也就是说去重的全排列就是从第一个数字起每个数分别与它后面非重复出现的数字交换。
class Solution { public: bool unique(vector<int> &num,int i,int index) { for(int j=index;j<i;j++) { if(num[j]==num[i]) return true; } return false; } void permute(vector<vector<int> > &result,vector<int> &num,int index) { if(index==num.size()) { result.push_back(num); } for(int i=index;i<num.size();i++) { if(unique(num,i,index)) continue; swap(num[i],num[index]); permute(result,num,index+1); swap(num[i],num[index]); } } vector<vector<int> > permuteUnique(vector<int> &num) { vector<vector<int> > result; result.clear(); permute(result,num,0); return result; } };
Permutations II,布布扣,bubuko.com
标签:style blog class code java color
原文地址:http://www.cnblogs.com/awy-blog/p/3705732.html