Given a collection of integers that might contain duplicates, nums, return all possible subsets.
Note:
- Elements in a subset must be in non-descending order.
- 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], [] ]
1 class Solution { 2 public: 3 vector<vector<int>> subsetsWithDup(vector<int>& nums) { 4 sort(nums.begin(),nums.end()); 5 vector<vector<int>> ans; 6 vector<int> cur; 7 ans.push_back(cur); 8 dep(ans,nums,0,cur); 9 return ans; 10 } 11 void dep(vector<vector<int>>&ans,vector<int>&nums,int pos,vector<int>&cur) 12 { 13 int size=nums.size(); 14 if(pos>=size) 15 return; 16 for(int i=pos;i<size;i++) 17 { 18 if(i!=pos&&nums[i]==nums[i-1]) 19 continue; 20 21 cur.push_back(nums[i]); 22 ans.push_back(cur);//解决自某个元素后都不选则添加不进结果集的情况 23 dep(ans,nums,i+1,cur); 24 cur.erase(cur.end()-1); 25 } 26 } 27 };