Leetcode longest Valid parentheses (C,c++,java,python)

Source: Internet
Author: User

Problem:

Given A string containing just the characters ‘(‘ ‘)‘ and, find the length of the longest valid (well-formed) parenthe SES substring.

for " (() ", the longest valid parentheses substring is " () ", which has length = 2.

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

Solution: Maintain a word Fu Yi and position stack, the word Fu Yi record does not match the character, the position stack record each of the characters in the stack position, if the stack is the right parenthesis, and the top of the stack is the left bracket, then the stack, to find out the current continuous fallback character length, to find out the maximum value is.
The main idea: to give a string, containing only the opening parenthesis and the closing parenthesis, to find the maximum length of the string legal parentheses.
Java source Code (350MS):
public class Solution {public    int longestvalidparentheses (String s) {        int len=s.length (), max=0;        stack<character> str_stack = new stack<character> ();        stack<integer> pos_stack = new stack<integer> ();        Char[] Str=s.tochararray ();        for (int i=0;i<len;i++) {            if (str[i]== ' (') {                str_stack.push (');                Pos_stack.push (i);            } else{                if (str_stack.size () >0 && Str_stack.peek (). Equals (' (')) {                    str_stack.pop ();                    Pos_stack.pop ();                    int tmp=pos_stack.size () ==0?i+1:i-pos_stack.peek ();                    Max=math.max (max,tmp);                } else{                    Str_stack.push (') ');                    Pos_stack.push (i);}}        }        return max;}    }

C Language Source code (3MS):
int longestvalidparentheses (char* s) {    int len=strlen (s), max=0,top=0,i,tmp;    int* pos_stack= (int*) malloc (sizeof (int) *len);    char* str_stack= (char*) malloc (sizeof (char) *len);    for (i=0;s[i];i++) {        if (s[i]== ' (') {            str_stack[top]= ' (';            pos_stack[top]=i;            top++;        } else{            if (str_stack[top-1]== ' (') {                top--;                if (top==0) tmp=i+1;                else tmp=i-pos_stack[top-1];                max=max>tmp?max:tmp;            } else{                str_stack[top]= ') ';                pos_stack[top]=i;                top++    ;    }}} return Max;}

C + + source code (7MS):
Class Solution {public:    int longestvalidparentheses (string s) {        int max=0,top=0,len=s.size ();        int* pos_stack= (int*) malloc (sizeof (int) *len);        char* str_stack= (char*) malloc (sizeof (char) *len);        for (int i=0;i<len;i++) {            if (s[i]== ' (') {                str_stack[top]= ' (';                pos_stack[top]=i;                top++;            } else{                if (str_stack[top-1]== ' (') {                    int tmp;                    top--;                    if (top==0) tmp=i+1;                    else tmp=i-pos_stack[top-1];                    max=max>tmp?max:tmp;                } else{                    str_stack[top]= ') ';                    pos_stack[top]=i;                    top++        ;        }}} return max;    };

Python source code (105MS):
Class solution:    # @param {string} s    # @return {integer}    def longestvalidparentheses (self, s):        length= Len (s); top=0; Max=0        str_stack=["For I in range (length)]        pos_stack=[0 for I in range (length)] for        i in range (length):            If s[i]== ' (':                str_stack[top]= ' ('                pos_stack[top]=i                top+=1            Else:                if top>0 and str_stack[ top-1]== ' (':                    top-=1                    tmp=i+1 if top==0 else i-pos_stack[top-1]                    Max=max if max>tmp else tmp                else:                    str_stack[top]= ') '                    pos_stack[top]=i                    top+=1        return Max


Leetcode longest Valid parentheses (C,c++,java,python)

Related Article

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.