标签:
Given a collection of integers that might contain duplicates, nums, return all possible subsets.
Note: The solution set must not contain duplicate subsets.
For example,
If nums = [1,2,2]
, a solution
is:
[ [2], [1], [1,2,2], [2,2], [1,2], [] ]
class Solution { public: vector<vector<int>> res; vector<vector<int>> subsetsWithDup(vector<int>& nums) { sort(nums.begin(),nums.end()); vector<int> t; DFS(nums,t,0); sort(res.begin(),res.end()); vector<vector<int>>::iterator it=unique(res.begin(),res.end()); //unique把重复的都扔掉后面,it指向的是所有不重复元素后的第一个位置 res.erase(it,res.end()); //删除it后的所有元素 return res; } void DFS(vector<int>& nums,vector<int>& cur,int k) { if(cur.size()==nums.size()) { res.push_back(cur); return; } else { res.push_back(cur); for(int i=k;i<nums.size();i++) { vector<int> tmp=cur; tmp.push_back(nums[i]); DFS(nums,tmp,i+1); } } } };
标签:
原文地址:http://blog.csdn.net/u011391629/article/details/52152199