Leetcode:subsets I & II

Source: Internet
Author: User

1 subset

Given a set of distinct integers, nums, 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 nums = [1,2,3] , a solution is:

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

Idea: For such a topic, already has an intuitive idea, is DFS, use recursion.
There are two kinds of recursive thinking, one is to split the problem into sub-problems, such as a subset of the first to seek [a subset of], according to the subset of [three]

Idea One

Can be used in the thought of recursion, observe s=[], S =[1], s = [1, 2] when the solution changes.

It is found that the solution of s=[1, 2] is to add 2 to the end of all the solutions of S = [1] and then to the original solution in S = [1]. So you can define vector<vector<int> > as the return result res, start with nothing in res, the first step into an empty vecotr<int>, and then iterate n times, each time the Res content is updated, Finally, return to Res.

classsolution{ Public: Vector<vector<int> > Subsets (vector<int> &S) {Vector<vector<int> >results; Vector<int>empty; if(S.empty ()) {results.push_back (empty); returnresults;        } sort (S.begin (), S.end ()); Vector<int>result; Result.push_back (s[0]);        Results.push_back (result);  for(inti =1; I < s.size (); i++) {vector<int>result;                        Result.push_back (S[i]); intSize =results.size ();  for(intj =0; J < size; J + +) {vector<int>R (Results[j].begin (), Results[j].end ());                R.push_back (S[i]);            Results.push_back (R);        } results.push_back (Result);        } results.push_back (empty); returnresults; }};

Another way of recursive thinking is to use Dfs

classsolution{ Public: Vector<vector<int> > Subsets (vector<int> &S) {Vector<vector<int> >results; Vector<int>result;        Results.push_back (result);        Sort (S.begin (), S.end ()); DFS (0, Result,s,results); returnresults; }    voidDfsintIndex, vector<int> & result, vector<int> & S, vector<vector<int> > &results) {         for(inti = index; I < s.size (); i++) {result.push_back (s[i]);            Results.push_back (result); DFS (i+1, Result,s,results);        Result.pop_back (); }    }};

2 subsets II

Given a collection of integers that might contain duplicates, Nums, 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 nums = [1,2,2] , a solution is:

[  2],  [1],  [1,2,2],  [2,2], [up  ],  []]

We take s=[1,2,2] as an example:

Can be found from s=[1,2] to s=[1,2,2], there are more than two subsets [2,2] and [1,2,2], these two subsets, in fact, [2], [all] the end is added 2 and produced. and [2], the two subsets are actually s=[1,2] of the solution to s=[1] The newly added part.

Therefore, if there are repeating elements in S, which can be sorted first, and if the current element s[i] and s[i-1] are the same, then it is different from the original idea of "adding s[i at the end of all your own copies of the current res", and we only copy a subset of the last addition in Res, Add s[i] at the end.

classsolution{ Public: Vector<vector<int> > Subsetswithdup (vector<int> &R) {Vector<vector<int> >results; Vector<int>empty;        Results.push_back (empty); if(S.empty ())returnresults;        Sort (S.begin (), S.end ()); intm =0; intStart =0;  for(inti =0; I < s.size (); i++){            if(I >0&& S[i] = = s[i-1]) {Start=m; }            Else{Start=0; }            intSize =results.size ();  for(intj = start; J < size; J + +) {vector<int>R (Results[j].begin (), Results[j].end ());                R.push_back (S[i]);            Results.push_back (R); } m=size; }        returnresults; }};

There is also another solution:

classSolution { Public: Vector<vector<int> > Subsetswithdup (vector<int> &S) {Vector<vector<int> >results; Vector<int>result;        Results.push_back (result);        Sort (S.begin (), S.end ()); DFS (0, Result,s,results); returnresults; }    voidDfsintIndex, vector<int> & result, vector<int> & S, vector<vector<int> > &results) {         for(inti = index; I < s.size (); i++){            if(i > Index && s[i] = = s[i-1])                Continue;            Result.push_back (S[i]);            Results.push_back (result); DFS (i+1, Result,s,results);        Result.pop_back (); }    }};

Leetcode:subsets I & 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.