【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.

【解析】

题意:给定一个包含左右括号的字符串,找出其中最长的有效子串,返回其长度。

思路:动态规划。用一个数组len[i]记录以i结尾的最长有效子串长度,如")()())"的动规数组为[0, 0, 2, 0, 4, 0]。具体做法是,从前往后遍历数组,遇到左括号,len[i]=0,遇到右括号,就往前找第一个多余的左括号的位置。首先跳过i前面的有效连续子串,碰到第一个没有匹配的括号p,如果p是右括号,那么p和i无法匹配,len[p]=0;如果p是左括号,那么p刚好和i匹配,此时要p到i都是有效子串,同时还要加上p前面连续的有效子串。

【Java代码】

public class Solution {
    public int longestValidParentheses(String s) {
        int longest = 0;

        int[] len = new int[s.length()];
        for (int i = 1; i < s.length(); i++) {
            if (s.charAt(i) == '(') {
                len[i] = 0;
            } else {
                // find the nearest parentheses before i that not matched
                int p = i - 1;
                while (p >= 0 && len[p] > 0) {
                    p -= len[p];
                }

                // if p is left parentheses, then it mathches i
                if (p >= 0 && s.charAt(p) == '(') {
                    len[i] = i - p + 1;

                    // add the length that matched before p
                    if (p > 0) {
                        len[i] += len[p - 1];
                    }

                    // update the parentheses length
                    longest = Math.max(longest, len[i]);
                }
            }
        }

        return longest;
    }
}
时间: 2024-12-15 01:39:56

【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 i

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