LeetCode47. Permutations II

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> permuteUnique(vector& nums) {
        vector> 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;
    }
};

你可能感兴趣的:(LeetCode47. Permutations II)