[Leetcode] Longest legal bracket longest valid parentheses

Source: Internet
Author: User
Tags valid

Give you a string that contains only ' (' and ') ' to find the longest valid bracket substring. For example: the longest legal parenthesis substring of "()" is "()" and the length is 2. ") () ())" is the longest legal parenthesis substring of "() ()", and the length is 4.

Problem Solving ideas 1:

    int longestvalidparentheses (string s) {
        //Start Typing your C + + solution below
        //do not write int main () funct Ion
        stack<int> left;//position of ' (' for
        (int II = 0; II < s.size (); ++ii) {
            if (s[ii] = = ' (') left.pu SH (ii);
            else if (!left.empty ()) {//') '
                s[ii] = ' k ';
                S[left.top ()] = ' k ';
                Left.pop ();
            }
        }
        int maxLength = 0;
        int length = 0;
        for (int II = 0; II < s.size (); ++ii) {
            if (s[ii]== ' K ') {
                ++length;                
                if (MaxLength < length) maxLength = length;
            }
            else length = 0;

        }
        return maxLength;
    }
;

2: The most easily thought of solution is the poor lifting method, the calculation of any two points (I to j) between the number of legitimate () string, with the help of dynamic programming can get results. The algorithm complexity is: O (n^3)

The solution to the O (n) requires a bit of skill, and the stack does not hold the ' (' instead ' (' Index '), which is based on several situations:
Every time I come ' (' after that, I press the stack unconditionally. If it encounters ') ', if the stack is not empty, the remaining ' (') in the stack is eliminated.
First: Eliminate ' (' after that, if there are remaining ' (') in the stack, the longest legal length is: maxLength = max (i-(int) st.top (), maxLength); That is, the index of the current ') ' minus the maximum value of both the index of the top element of the stack and the original max_length.

For example: For this case: () ((), the maximum value of 4 can be correctly obtained.

Second: Eliminate ') ', there is no remaining ' (' in the stack. A new variable start is introduced to represent the starting point of the legal parenthesis string.
For example: For this case: ()) () (), the maximum value of 4 can be correctly obtained.

Start is the index of the current ') ' When the stack is empty, starting at 1, and then each time it encounters ') '. That is, cannot be eliminated) the parentheses cannot be combined with the preceding parentheses to calculate the longest sequence, so update start.

Code:

    int longestvalidparentheses (string s) {  
        if (s.length () ==0) {  
            return 0;  
        }  
        int start     =-1;  
        int maxLength = 0;  
        Stack<int> St;  
        for (int i=0;i<s.length (); i++) {  
            if (s.at (i) = = ' (') {  
                st.push (i);  
            } else {  
                if (!st.empty ()) {  
                    St.pop ();  
                    if (St.empty () ==true) {  
                        maxLength = max (I-start, maxLength);  
                    } else {  
                        maxLength = max (i-(int) st.top (), maxLength);  
                    }  
                } else {  
                    start = i;}}  
        }  
      
        return maxLength;  
    



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.