【leetcode】32. Longest Valid Parentheses

这题不能使用递归来写,因为有一个输入长度是17173,会爆栈。因此得手动模拟栈调用。

public class Solution {
    private static class StackFrame {
        public final char left;
        private int num;
        public StackFrame(char left) {
            this.left = left;
        }
        @Override
        public String toString() {
            return "StackFrame [left=" + left + ", num=" + num + "]";
        }
    }

    private static final Stack<StackFrame> stack = new Stack<StackFrame>();

    public int longestValidParentheses(String s) {
        stack.clear();
        stack.push(new StackFrame(‘#‘));
        int maxNum = 0;

        for (int i = 0; i < s.length(); ++i) {
            char c = s.charAt(i);
            if (isLeft(c)) {
                stack.push(new StackFrame(c));
            } else if (isPair(stack.peek().left, c)) {
                StackFrame frame = stack.pop();
                stack.peek().num += frame.num + 2;
                if (stack.peek().num > maxNum) {
                    maxNum = stack.peek().num;
                }
            } else {
                stack.peek().num = 0;
                if (stack.size() > 1) {
                    while (stack.size() > 1) {
                        stack.pop();
                    }
                }
            }
        }
        return maxNum;
    }

    private boolean isLeft(char c) {
        return c == ‘(‘ || c == ‘[‘ || c == ‘{‘;
    }

    private boolean isPair(char left, char right) {
        if (left == ‘(‘) {
            return right == ‘)‘;
        } else if (left == ‘[‘) {
            return right == ‘]‘;
        } else {
            return right == ‘}‘;
        }
    }
}
时间: 2024-08-28 06:46:12

【leetcode】32. Longest Valid Parentheses的相关文章

【leetcode with java】32 Longest Valid Parentheses O(n)

这个题目leetcode上提示用动态规划,但是那样要O(n^2).我自己想出了一个O(n)的算法,并提交通过. [题目] 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

[Leetcode][Python]32: Longest Valid Parentheses

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 32: Longest Valid Parentheseshttps://oj.leetcode.com/problems/longest-valid-parentheses/ Given a string containing just the characters '(' and ')',find the length of the longest valid (well-fo

LeetCode - 32. Longest Valid Parentheses

32. Longest Valid Parentheses Problem's Link ---------------------------------------------------------------------------- Mean: 给定一个由'('和')'组成的字符串,求最长连续匹配子串长度. analyse: 定义一个stack<pair<char,int>>类型的stack. 遇到'('进栈; 遇到')'需要分两种情况讨论: 栈顶元素为'(',正好匹配,

刷题32. Longest Valid Parentheses

一.题目说明 题目是32. Longest Valid Parentheses,求最大匹配的括号长度.题目的难度是Hard 二.我的做题方法 简单理解了一下,用栈就可以实现.实际上是我考虑简单了,经过5次提交终于正确了. 性能如下: Runtime: 8 ms, faster than 61.76% of C++ online submissions for Longest Valid Parentheses. Memory Usage: 9.8 MB, less than 10.71% of

Java [leetcode 32]Longest Valid Parentheses

题目描述: 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 exampl

LeetCode 32 Longest Valid Parentheses(最长有效括号)(*)

翻译 给定一个仅仅包含"("或")"的字符串,找到其中最长有效括号子集的长度. 对于"(()",它的最长有效括号子集是"()",长度为2. 另一个例子")()())",它的最长有效括号子集是"()()",其长度是4. 原文 Given a string containing just the characters '(' and ')', find the length of the l

leetCode 32.Longest Valid Parentheses (有效的最大括号) 解题思路和方法

Longest Valid Parentheses 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

LeetCode 32 Longest Valid Parentheses (C,C++,Java,Python)

Problem: 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 exa

[LeetCode] 32. Longest Valid Parentheses 最长有效括号

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 &