美文网首页
LeetCode47. Permutations II

LeetCode47. Permutations II

作者: Yuu_CX | 来源:发表于2016-11-16 20:56 被阅读0次

    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;
        }
    };
    

    相关文章

      网友评论

          本文标题:LeetCode47. Permutations II

          本文链接:https://www.haomeiwen.com/subject/vegrpttx.html