32. Longest Valid Parentheses

https://leetcode.com/problems/longest-valid-parentheses/description/
解题思路:用stack来解决

class Solution {
public int longestValidParentheses(String s) {

    Stack stack = new Stack();
    int res = 0;
    int count = 0;
    for (int i = 0 ; i < s.length(); i++){
        if(s.charAt(i)=='('){
            stack.push(count);
            count = 0;
        }else{
            if(!stack.isEmpty()){
                count = count + 1 + stack.peek();
                stack.pop();
                res = Math.max(res, count);
            }else{
                count = 0;
            }
        }
    }
    return res*2;
}

}

你可能感兴趣的:(32. Longest Valid Parentheses)