[Leetcode] subsets [31]

Source: Internet
Author: User

Topic

Given a set of distinct integers, S, return all possible subsets.

Note:

    • Elements in a subset must is in non-descending order.
    • The solution set must not contain duplicate subsets.

For example,
If S = [1,2,3] , a solution is:

[  3],  [1], [2], [1,3], [+]  ,  [2,3], [up  ],  []]

Link to original topic (point me)

Thinking of solving problems

Sub-collection issues. Give a set to find out all of its subsets. This question is also a combinatorial problem-the old idea: recursive plus cycle. The understanding of such a topic is to find a practical example, and then simulate a layer of recursion, a layer of uniform, hands-on understanding 2 times, this type of problem, basically there are ideas.

Code implementation
Class Solution {public:    vector<vector<int> > Subsets (vector<int> &s) {        vector< vector<int> > ret;        Sort (S.begin (), S.end ());        Helper (0, S, vector<int> (), ret);        return ret;    }        void helper (int start, const vector<int>& S, vector<int> part, vector<vector<int> >& ret {        if (start = = S.size ()) return;        if (start==0) Ret.push_back (part);        for (int i=start; i<s.size (); ++i) {            part.push_back (s[i]);            Ret.push_back (part);            Helper (i+1, S, part, ret);            Part.pop_back ();}}    ;

And another very similar topic for this question, subset Ii.


If you think this article has a harvest for you, please help the top.
In addition, I opened the public number- sharing the beauty of technology , I will not regularly share some of my learning things.You can search the public number: Swalgeor scan the QR code below to follow me .
(Reprint article Please specify Source: http://blog.csdn.net/swagle/article/details/30219965)

[Leetcode] subsets [31]

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.