Leetcode No90. Subsets II

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.