Leetcode | | 90, subsets II

Source: Internet
Author: User

Problem:

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

Hide TagsArray BacktrackingTest instructions: Finds all sub-sequences (containing empty set) of an array with repeating elements, and the subsequence cannot be duplicated.

Thinking:

(1) My idea is to ask for a subsequence of length k first, k from 0 to N. The previous question subset also uses this idea, but does not have the repetition element.

(2) The length of the sub-sequence of K: Dfs deep Search, that is, backtracking, open a K-size array to record the traversed elements.

(3) The focus is on how to go heavy. Online to see a good idea: open an array to record the use of repeating elements, the current element is the same as the previous element,

If the previous element is not used, the element cannot be used, which is used after the previous element is used. Corresponds to the order in which the repeating elements are used,

The repetition of the subsequence is avoided.

(4) I also try to use the STL unique_copy () function, but the function is only the deduplication of adjacent repeating elements, if the same subsequence is not next to each other, it is not a good sort, it can not be heavy.

If you really want to do this, consider using a string to store the subsequence, which you can use to Unique_copy ().

Code

Class solution {private:vector<vector<int> > ret;    vector<int> tmp; BOOL canuse[100];p ublic:vector<vector<int> > Subsetswithdup (vector<int> &s) {ret.clear ()            ;            unsigned int n=s.size ();            if (n==0) return ret;            Sort (S.begin (), S.end ());            Tmp.clear ();            Ret.push_back (TMP);            memset (canuse,true,sizeof (bool) *100);                for (int k=1;k<=n;k++) {tmp.resize (k);            DFS (0,n,s,k,0);            } return ret;               } protected:void dfs (int dep, int n, vector<int> &s,int k,int start) {                   if (dep==k) {ret.push_back (TMP);               Return                    } for (int i=start;i<n;i++) {if (i==0) {                    Canuse[i]=false; tmp[dep]=S[i];                    DFS (DEP+1,N,S,K,I+1);                   Canuse[i]=true;                           } else {if (S[i-1]==s[i] && canuse[i-1])                       Continue                       Canuse[i]=false;                       Tmp[dep]=s[i];                       DFS (DEP+1,N,S,K,I+1);                   Canuse[i]=true; }               }            }};


Leetcode | | 90, 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.