[Leetcode] Subsets Sub-collection

Source: Internet
Author: User

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  ],  []]

The question of this collection of children, because it is to list all the results, according to previous experience, must be used to return to do. This problem in fact its non-recursive solution is relatively simple, the following we first look at the non-recursive solution, because the topic requires the sub-set of numbers in the order of non-descending arrangement, all we need to pre-processing, the input array to sort, and then further processing, at first I think, is to be in accordance with the length of the subset is written out from less to more, such as the subset length of 0 is the empty set, the empty sets are a subset of any collection, meet the conditions, directly join. The following length is a subset of 1, directly a loop to join all the numbers, the subset length of 2 can be used two loops, but this idea to the back of the impossible, because the number of loops can not grow infinitely, so we must change a way of thinking. We can be one of the online overlay, such as the topic of the example [three-to-one], the first is an empty set, then we have to deal with 1, just add 1 to the empty set, for [1], now we have two ourselves [] and [1], we will deal with 2, we on the basis of the previous subset, each can be obtained separately [2],[1, 2], then all the subsets are now [], [1], [2], [1, 2], the same treatment 3 is possible [3], [1, 3], [2, 3], [1, 2, 3], plus the previous subset is all subsets, the code is as follows:

//non-recursionclassSolution { Public: Vector<vector<int> > Subsets (vector<int> &S) {vector<vector<int> > Res (1);        Sort (S.begin (), S.end ());  for(inti =0; I < s.size (); ++i) {intSize =res.size ();  for(intj =0; J < size; ++j) {Res.push_back (res[j]);            Res.back (). push_back (S[i]); }        }        returnRes; }};

The entire order of additions is:

[]
[1]
[2]
[1 2]
[3]
[1 3]
[2 3]
[1 2 3]

The following is a recursive solution, equivalent to a depth-first search, see Netizen justdoit Blog, because the original set of each number only two states, either exists, or does not exist, then in the construction of a subset of the choice and do not choose two cases, so you can construct a binary tree, The Zuozi indicates that the node selected for the layer processing, the right subtree means no selection, the final leaf node is all subcollections, the code is as follows:

//recursionclassSolution { Public: Vector<vector<int> > Subsets (vector<int> &S) {vector<vector<int> >Res; Vector<int> out;        Sort (S.begin (), S.end ()); Getsubsets (S),0, out, RES); returnRes; }    voidGetsubsets (vector<int> &s,intPOS, vector<int> & out, vector<vector<int> > &Res) {Res.push_back ( out);  for(inti = pos; I < s.size (); ++i) { out. Push_back (S[i]); Getsubsets (S, I+1, out, RES);  out. Pop_back (); }    }};

The entire order of additions is:

[]
[1]
[1 2]
[1 2 3]
[1 3]
[2]
[2 3]
[3]

[Leetcode] Subsets Sub-collection

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.