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], [2,1,1]]
class Solution {
public:
vector<vector<int>> permuteUnique(vector<int>& nums) {
vector<vector<int>> res;
if(nums.empty()) return res;
sort(nums.begin(),nums.end());
res.push_back(nums);
//next_permutation会自动过滤掉重复的
while(next_permutation(nums.begin(),nums.end()))
{
res.push_back(nums);
}
return res;
}
};
网友评论