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的思想,用d[i]表示从第i个位置开始匹配的长度,那么对于第i个来说,如果它是右括号的话,那么这个位置就是0,如果是做括号的话,那么就要跳过第i+1匹配的最长长度的位置来到j,看位置j是不是右括号,其次还要加上位置j+1的位置的匹配长度。

class Solution {
public:
    int longestValidParentheses(string s) {
        if (s.length() == 0) return 0;

        int ans = 0;
        int *d = new int[s.length()];
        for (int i = 0; i < s.length(); i++)
            d[i] = 0;
        d[s.length() - 1] = 0;
        for (int i = s.length() - 2; i >= 0; i--) {
            if (s[i] == ')')
                d[i] = 0;
            else {
                int j = i + 1 + d[i + 1];
                if (j < s.length() && s[j] == ')') {
                    d[i] = d[i+1] + 2;
                    if (j + 1 < s.length())
                        d[i] += d[j+1];
                }
            }
            ans = max(ans, d[i]);
        }

        return ans;
    }
};
时间: 2024-10-19 08:43:05

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 [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 解题思路

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(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 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 动态规划

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分析和实现

题目大意:给出一个只包含字符'('和')'的字符串S,求最长有效括号序列的长度. 很有趣的题目,有助于我们对这种人类自身制定的规则的深入理解,可能我们大多数人都从没有真正理解过怎样一个括号序列是有效的,因此解题也无从说起.整道题目的难度在于我们对有效括号序列的理解和定义.下面给出我自己的定义:. 定义1:空括号序列是有效的. 定义2:对于一对左右括号,若左括号出现在右括号的左边,且左右括号之间(不包含两端)的括号序列是有效的,那么称该左括号到该右括号(包含)这一段序列是有效的.且称该左括号和右括