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相比目派,數(shù)組中加入了重復(fù)的元素,在最后刪除數(shù)組中重復(fù)元素即可筋粗。
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;
? ? }