LeetCode 32. Longest Valid Parentheses (Valid Max brackets) solution ideas and methods

Source: Internet
Author: User

LeetCode 32. Longest Valid Parentheses (Valid Max brackets) solution ideas and methods

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 is (), which has length = 2.

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

 

Idea: refer to the online solution for this question. What I made myself was the time complexity of loop O (n ^ 3), and the submission timed out decisively. The idea of my solution is to first judge whether the string is an odd or even number, and the even number begins with this string, to determine whether it is a valid pair of parentheses, whether it is a return, no, the length is reduced by 2, and the s is intercepted cyclically, until the maximum value is found.

Another method is to refer to other people's ideas. The overall idea of self-writing code is to traverse S cyclically and save it with two stacks, one is to save "()" and the other is to save the index, the operations on the two stacks are the same. At last, the elements that do not have an output Stack are unmatched elements, which are also the demarcation points of each valid bracket group. Based on this, each index can subtract the maximum value.

For the same complex string, method 1 takes 1790 ms, and method 2 takes 1 ms. The efficiency gap is huge.

Method 1 code:

 

Public int longestValidParentheses (String s) {int len = s. length (); if (len <= 1) {return 0;} int startLen; int validLen = 0; // The length is an even number 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;} // specifies whether valid parentheses are returned. len is returned.-1 private int lenValid (String str) {Stack is returned.
 
  
St = new Stack
  
   
(); 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 2 code:

 

 

Public int longestValidParentheses1 (String s) {Stack
 
  
St = new Stack
  
   
(); // Save () Stack
   
    
Si = new Stack
    
     
(); // Save () The index si. push (-1); // use-1 as a demarcation start value for (int I = 0; I <s. length (); I ++) {if (st. isEmpty () | st. peek ()! = '(' | S. charAt (I )! = ') {St. push (s. charAt (I); // input stack si. push (I);} else {st. pop (); // output stack si. pop () ;}// si. push (s. length ()-1); // each element without an output stack is the demarcation point of each valid group. int end = s. length (); // start 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 ;}
    
   
  
 



 

 

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.