LeetCode—90. Subsets II

Given a collection of integers that might contain duplicates, nums, return all possible subsets (the power set).

Note: The solution set must not contain duplicate subsets.


与之前的subsets相比,数组中加入了重复的元素,在最后删除数组中重复元素即可。


vector<vector<int>> subsetsWithDup(vector<int>& nums) {

        int n = nums.size();

        vector<vector<int>> res(1);

        sort(nums.begin(), nums.end());

        for(int i=0; i<n; i++){

            int size = res.size();

            for(int j=0; j<size; j++){

                res.push_back(res[j]);

                res.back().push_back(nums[i]);

            }

        }

        sort(res.begin(), res.end());

        res.erase(unique(res.begin(), res.end()), res.end());

        return res;

    }

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容