leetCode4.1.2(Longest Valid Parentheses)

来源:互联网 发布:手机淘宝1元秒杀入口 编辑:程序博客网 时间:2024/05/14 15:41

Given a string containing just the characters ’(’ and ’)’, find the length of the longest valid (wellformed) 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.


public static int solution_4_1_2(char[] chars){int start=0;int[] st=new int[chars.length];int top=0;int max=0;for(int i=0;i<chars.length;i++){if(chars[i]=='(')st[top++]=i;if(chars[i]==')'){if(top==0){start=i+1;}else{//int t=--top;if(top==0){max=max>=(i-start+1)?max:i-start+1;}else{max=max>=(i-st[top-1])?max:i-st[top-1];}}}}return max;}



0 0