[LeetCode]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 ")()())", where the longest valid parentheses substring is "()()", which has length = 4.、

思路

网上有牛人碰到这种问题第一反应就是DP,而我赶紧用栈处理这种括号匹配更简单一些,毕竟当初数据结构课上将栈匹配括号还是花了不少时间的。

这里直接用栈计算匹配括号的数量,然后 × 2得到长度是不对的,因为题目求的是连续最长括号匹配数,突出连续两个字。

因此,我们可以采用“标记”的方法,用栈维持左括号的下标,遇到一对匹配的括号,可以将其对应()在原先字符串的位置赋值一个特殊字符‘a‘,匹配结束后,计算连续a的长度即可。

AC代码

public class Solution {
    public int longestValidParentheses(String s) {
        Stack<Integer> stack = new Stack<Integer>();
        StringBuilder sBuilder = new StringBuilder(s);

        for (int i = 0; i < s.length(); i++) {
            if (sBuilder.charAt(i) == ‘(‘) {
                stack.push(i);
            } else {
                if (stack.isEmpty()) {
                    continue;
                } else {
                    sBuilder.setCharAt(i, ‘a‘);
                    sBuilder.setCharAt(stack.pop(), ‘a‘);
                }
            }
        }

        int max = 0, len = 0;
        for (int i = 0; i < sBuilder.length(); i++) {
            if (sBuilder.charAt(i) == ‘a‘) {
                len++;
                max = Math.max(max, len);
            } else {
                len = 0;
            }
        }

        return max;
    }
}

[LeetCode]Longest Valid Parentheses, 解题报告,布布扣,bubuko.com

时间: 2024-12-18 10:34:03

[LeetCode]Longest Valid Parentheses, 解题报告的相关文章

【LeetCode】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

[LeetCode] 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 &

LeetCode: Longest Valid Parentheses [031]

0.下载安装Opencv,当前版本为249. 1.下载Python,当前OPencv版本为249,不过其支持的最新版本的Python为2.7,所以可以下载276版本. 2.下载numpy,开始我使用了1.6,没有通过,错误如图.下载了最新的1.8.1版本. 3.将Opencv安装目录下opencv\build\python\2.7\x86中的cv2.pyd复制到python安装目录Lib\site-packages下. 4.找到opencv源文件内的draw.py运行. LeetCode: Lo

Leetcode 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 &

LeetCode: Longest Valid Parentheses O(n)时间 O(1)空间

题目: 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

LeetCode -- Longest Valid Parentheses(Dynamic Programming)

题目地址:https://leetcode.com/problems/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

LeetCode 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 &

[LeetCode] 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 &

[LeetCode] 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 &