[LeetCode]Longest Valid Parentheses

来源:互联网 发布:大数据和电子政务 编辑:程序博客网 时间:2024/06/04 23:28

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.

public class Solution {    public int longestValidParentheses(String s) {    if(s==null||s.length()==0){    return 0;    }    Stack<Integer> stack = new Stack<Integer>();    int lastposition = -1;// the last  not match position of ")"    int res = 0;    for(int i =0;i<s.length();i++){    String str = s.substring(i,i+1);    if(str.equals("(")){    stack.push(i);    }else{    if(stack.isEmpty()){    lastposition = i;    }else{    stack.pop();    if(stack.isEmpty()){    res = Math.max(res, i-lastposition);    }else{    res = Math.max(res, i-stack.peek());//s.peek()== last not match postion of "("    }    }     }    }        return res;    }}




0 0
原创粉丝点击