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

Hide Tags

Dynamic Programming String


  其实就是判断最常合法长度

如果当前字符为‘(‘ 忽略。

如果为 ‘)‘:

1. 前一字符为 ‘(‘,那么长度是前二字符的最长合法长度+2.

2. 前一字符为‘)‘,那么获得前一字符的最长合法长度tmpLen

  a. 如果 前tmpLen+1  项是‘(‘,那么便是合法的,取值为前tmpLen+1+1 项的长度  + tmpLen+2.

  b. 为‘)‘,配对失败,忽略。

最后返回最长的。

#include <string>
#include <iostream>
#include <vector>
using namespace std;

class Solution {
public:
    int longestValidParentheses(string s) {
        int len = s.length();
        if(len<1)   return 0;
        vector<int > table(len+1,0);
        int cnt = 0;
        if(s[0]==‘(‘)   cnt++;
        else    cnt --;
        int retMax = 0;
        for(int i=1;i<len;i++){
            if(s[i]==‘(‘){
                if(cnt<0)   cnt=1;
                else    cnt++;
                continue;
            }
            cnt--;
            if(cnt>=0){
                if(s[i-1]==‘(‘) table[i+1] = table[i-1]+2;
                else{
                    if(s[i-1-table[i]]==‘(‘)
                        table[i+1] = table[i-1-table[i]]+2+table[i];
                }
                if(retMax<table[i+1])   retMax = table[i+1];
            }
        }
        return retMax;
    }
};

int main()
{
    Solution sol;
    cout<<sol.longestValidParentheses("()(())")<<endl;
    return 0;
}
时间: 2024-12-29 01:47: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 [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 最长有效括号

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

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:对于一对左右括号,若左括号出现在右括号的左边,且左右括号之间(不包含两端)的括号序列是有效的,那么称该左括号到该右括号(包含)这一段序列是有效的.且称该左括号和右括