美文网首页
32. Longest Valid Parentheses

32. Longest Valid Parentheses

作者: becauseyou_90cd | 来源:发表于2018-07-28 02:06 被阅读0次

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

    class Solution {
    public int longestValidParentheses(String s) {

        Stack<Integer> stack = new Stack<Integer>();
        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

          本文链接:https://www.haomeiwen.com/subject/hzcnmftx.html