Question:
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], [up ], []]
algorithm:BFS Breadth First search, then remove duplicateAccepted Code:
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 ()); The unique one throws the duplicates back, it points to the first position after all the non-repeating elements res.erase (it,res.end ()); Remove all elements after 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); } } }};
Leetcode No90. Subsets II