[C + +] leetcode:64 subsets II

Source: Internet
Author: User

Topic:

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

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

On the basis of the previous question subsets, allow to appear in the collection contains duplicate numbers, with the collection {1,2,2} as an example, draw two fork tree

Answer 1:dfs Solution

idea: to parse a binary tree, we know to handle the third element 2 o'clock, because the front has been dequeue once 2, so in the third layer, we only added 2 of the binding {and {} and {2} and then add 2, instead of adding 2 in the collection {1},{} (those branches that draw the fork), Assuming that there is a 2 below, we will only add 2 to the collection {12,2} and {2,2} that contain two 2 in the fourth layer, and none of the others are added. So DFS, if the number that is currently being processed, appears in front of K (in the original set S), then we have to deal with the set of K that must contain the element. Otherwise, a duplicate collection will appear.


Attention:

1. First to calculate the current element appears in the original set several times, the calculation of K. This first can be positioned to the first and s[ileaf] the same number of positions, and then the difference to get the number of repetitions.

<span style= "FONT-SIZE:14PX;" >//fstname points to the first and s[ileaf] the same number position        int fstname = ileaf;        do{            fstname--;        } while (s[fstname] = = S[ileaf]);        fstname++;                int samenum = Ileaf-fstname; Number of repetitions, not including s[ileaf] itself </span>


2. Determine whether the subset contains K repeating elements , because the subset is ordered, so directly to the corresponding sub-set of the first occurrence of the duplicate number of the position, determine whether equal to s[ileaf]. If the same, the description starts from this position, there are k of the repeating element in the sub-set.

<span style= "FONT-SIZE:14PX;" >//If this round does not contain duplicate numbers, or if the number that is currently being processed is preceded by K times, and the collection to be processed contains k of that element. Add S[ileaf]        if (samenum = = 0 | | (Tmpres.size () >= samenum && tmpres[tmpres.size ()-samenum] = = S[ileaf]))        {            tmpres.push_back (s[ileaf]);            Subsetswithdup_helper (S, Ileaf + 1, tmpres, ret);            Tmpres.pop_back ();        } </span>

AC Code:

Class Solution {public:vector<vector<int> > Subsetswithdup (vector<int> &s) {Vector<ve        ctor<int>> ret;        if (s.size () = = 0) return ret;        Vector<int> Tmpres;        Sort (S.begin (), S.end ());        Subsetswithdup_helper (S, 0, Tmpres, ret);    return ret; } private:void Subsetswithdup_helper (vector<int>& S, int ileaf, vector<int>& tmpres, vector<            vector<int>>& ret) {if (Ileaf = = S.size ()) {ret.push_back (tmpres);        Return        }//fstname points to the first and s[ileaf] the same number position int fstname = Ileaf;        do{fstname--;        }while (S[fstname] = = S[ileaf]);                fstname++; int samenum = Ileaf-fstname; Number of repetitions, not including s[ileaf] itself//If this round process does not contain duplicate numbers, or if the number that is currently being processed is preceded by K times, and the collection to be processed contains k of that element. Add S[ileaf] if (samenum = = 0 | |       (Tmpres.size () >= samenum && tmpres[tmpres.size ()-samenum] = = S[ileaf])) {Tmpres.push_back (s[ileaf]);            Subsetswithdup_helper (S, Ileaf + 1, tmpres, ret);        Tmpres.pop_back ();                } subsetswithdup_helper (S, Ileaf + 1, tmpres, ret);    Return }};

Answer 2:

Thinking Analysis:

Process an element in two cases:

1) If the currently processed element does not appear, add the element to the previous collection and double the number of sets.

2) If the currently processed element has occurred, then we will only add the last round of the processed set plus the element (that is, the collection of the current repeating number added to the previous round). For example, to add a second 2 o'clock, we only add the last round of the 2 collection {"}, {2} and add another 2 to the result, the other collections do not understand, {1} and {} are inherited from the previous layer directly, do not do processing." Since we are sequentially adding collections to the RET, the processed collections are always in the back position of the RET.

Attention:

1. Two variables need to be maintained, the last number processed, that is, the number of subsets to be operated on.

<span style= "FONT-SIZE:14PX;" >//if this processing number and the last do not repeat, update both variables if            (s[i]! = Last            ) {previous                = s[i];                Opresnum = Ret.size ();            } </span>
2. If this processing element repeats, the opresnum will not be updated, then ret.size ()-Opresnum is not equal to 0, we only process the res.size ()-Opresnum collection later.

<span style= "FONT-SIZE:14PX;" >//If duplicate numbers occur, the number of collections in this process is the same as the last, that is, only the last processed collection is processed. Ressize-opresnum, if Opresnum is not equal to Res.size (), only the last processed collection is processed. Because iterations are added sequentially, the last processed collection must be at the end of the RET res.size ()-Opresnum.            int retsize = Ret.size ();            for (int j = retSize-1; J >= Retsize-opresnum; j--)            {                ret.push_back (ret[j]);                Ret.back (). push_back (S[i]);            } </span>

AC Code:

Class Solution {public:    vector<vector<int> > Subsetswithdup (vector<int> &s) {        int len = S.size ();        Sort (S.begin (), S.end ());        vector<vector<int>> ret (1);        The last processed number, which is the number of subsets that will be manipulated        , int last = S[0], opresnum = 1;                for (int i = 0; i < s.size (); i++)        {            //if this processing number and the last is not repeated, update both variables if            (s[i]! =)            {Previous                = S[i];
   opresnum = Ret.size ();            }                        If duplicate numbers occur, the number of collections in this process is the same as the last one, that is, only the last processed collection is processed. Ressize-opresnum, if Opresnum is not equal to Res.size (), only the last processed collection is processed. Because iterations are added sequentially, the last processed collection must be at the end of the RET res.size ()-Opresnum.            int retsize = Ret.size ();            for (int j = retSize-1; J >= Retsize-opresnum; j--)            {                ret.push_back (ret[j]);                Ret.back (). push_back (S[i]);            }        }                return ret;    }};



[C + +] leetcode:64 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.