# "Leetcode" Generate parentheses (middle) ☆

Source: Internet
Author: User

Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.

For example, given n = 3, a solution set is:

"((()))", "(()())", "(())()", "()(())", "()()()"

Idea: Generate all reasonable pairs of parentheses

I use the backtracking method, traverse all the press into the K ') ' Before the ' (' number of

classSolution { Public: Vector<string> Generateparenthesis (intN) {vector<string>ans; if(n = =0)            returnans; Vector<vector<Char>> S (n);//the number of opening parentheses that have been placed when you put the K right parenthesis        intK =0;  for(inti = k +1; I <= N; i++) {s[k].push_back (i); }        stringX;  while(k >=0)        {             while(!S[k].empty ()) {                intNumofleftp = Getnumofleft (X);//number of left brackets not yet put in                 while(Numofleftp < S[k].back ())//if X "(" less than required, press in "("{x.append ("("); Numofleftp++; }                 while(Numofleftp > S[k].back ())//if X "(" More than needed, the popup                {                    Charback =X.back ();                    X.pop_back (); if(Back = ='(') {numofleftp--; }} x.append (")");//Press into the new ")"                intback =S[k].back ();                S[k].pop_back (); if(K < n-1) {k++; intnum = Max (back, K +1);//the number of open parentheses that you can select must be greater than the current existing less than or equal to n                     for(inti = num; I <= N; i++) {s[k].push_back (i); }                }                Else{ans.push_back (X); }} k--; }        returnans; }    intGetnumofleft (stringX) {intposition=0; intI=0;  while((Position=x.find_first_of ("(", position))! =string:: NPOs) {Position++; I++; }          returni; }};

My thinking is very cumbersome, in the middle to make a variety of judgments, look at the great God.

Create parentheses with a length of 2*n, but not randomly generated

Set Len is the current string length, and V is the current full pair of parentheses logarithm

If Len-v < n can also be put in ' ('

If 2 * v < Len can also be put in ') '

The answer is pressed when the length meets the criteria, and only then the stack length is reduced, and the new value is pressed into the other lengths.

vector<string> GenerateParenthesis2 (intN) {vector<string>ans; Vector<string>Stack; Vector<int>Validationstack; Stack.push_back ("("); Validationstack.push_back (0);  while(Stack.size ()! =0)        {            strings =Stack.back ();            Stack.pop_back (); intv =Validationstack.back ();            Validationstack.pop_back (); if(s.length () = =2*N) {ans.push_back (s); Continue; }            if(S.length ()-V <N) {stack.push_back (S.append ("("));                Validationstack.push_back (v);            S.pop_back (); }            if(2* V <s.length ()) {Stack.push_back (S.append (")")); Validationstack.push_back (v+1);            S.pop_back (); }        }        returnans; }

"Leetcode" Generate parentheses (middle) ☆

Related Keywords:

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.