Leetcode 32.Longest Valid parentheses (effective maximum bracket) thinking and method of solving problems

Source: Internet
Author: User

Longest Valid parentheses


Given A string containing just the characters ' (' and ') ', find the length of the longest valid (well-formed) parentheses Substring.

For "(()", the longest valid parentheses substring are "()", which has length = 2.

Another example is ") () ()", where the longest valid parentheses substring are "() ()", which has length = 4.


Idea: This problem is also see the online data solution. I made it myself. The time complexity of the loop O (n^3), commit a decisive timeout. My solution to the idea is to determine whether the string is an odd or even, even the beginning of this string, to determine if it is a valid pair of parentheses, is returned, not, the length minus 2, the loop intercept s until the maximum is found.

Another way to see other people's ideas, their own code, the overall idea is to loop through S, with two stacks to save, a Save "()", a save index, two stacks of the same operation. The last element that is not out of the stack is an element that cannot be matched, and is also the cutoff point for each valid bracket group, whereby the maximum value can be subtracted from each index.

The same complex string, the method is time-consuming 1790ms, method two time-consuming 1ms. The efficiency gap is huge.

Method one code:

    public int longestvalidparentheses (String s) {int len = s.length ();        if (len <= 1) {return 0;        } int Startlen;        int validlen = 0;        The length is even if (len & 1) = = 0) {Startlen = Len;        }else{Startlen = len-1;        } Boolean isbreak = false;            while (Startlen > 0) {if (isbreak) break;                for (int i = 0; i + startlen <= len; i++) {String temp = s.substring (I,i+startlen);                int k = Lenvalid (temp);                    if (k > Validlen) {validlen = k;                    Isbreak = true;                Break        }} Startlen-= 2;    } return Validlen; }//str valid parentheses, valid return Len, invalid return-1 private int lenvalid (String str) {stack<character> st = new Stack<chara        Cter> (); for (int i = 0; i< str.length (); i++) {if (St.isempty () | | St.peek ()! = ' (' | | sTr.charat (i)! = ') ') {St.push (Str.charat (i));            }else{St.pop ();        }} if (St.isempty ()) {return str.length ();    } return-1; }
Method two code:

    public int longestValidParentheses1 (String s) {        stack<character> st = new Stack<character> ();//Save ()        stack<integer> si = new stack<integer> ();//Save () index        Si.push (-1);//1 as a cutoff starting value for        (int i = 0; I < s.length (); i++) {            if (st.isempty () | | St.peek ()! = ' (' | | | s.charat (i)! = ') ') {                St.push (S.charat (i));//Into Stack                si.push (i);            } else{                St.pop ();//Stack                si.pop ();            }        }        Si.push (S.length ()-1);        Each non-stack element is the dividing point of each valid group        int end = S.length ();//starting point        int max = 0;//Maximum length while        (!si.isempty ()) {            int Start = Si.pop ();            max = end-start-1 > Max? End-start-1:max;            end = start;        }        return max;    }



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Leetcode 32.Longest Valid parentheses (effective maximum bracket) thinking and method of solving problems

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.