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[i]表示从s[i]开始(包括s[i]),到达s[n-1]时,最长的有效括号对的长度。
 
如果s[i]=='('
我们需要检查j=dp[i+1]+i+1对应的s[j]位置的括号是否为")",如果s[j]为')'则说明又组成了一对括号
故此时dp[i]=dp[i+1]+2
于此同时,我们还需要继续考虑dp[j+1]的值,如果j+1没有超出范围,则dp[i]=dp[i+1]+2+dp[j+1]
 
其他情况dp[i]=0;
 
 class Solution {
public:
int longestValidParentheses(string s) {
int n=s.length();
if(n==) return ; int *dp=new int[n];
dp[n-]=; int result=;
for(int i=n-;i>=;i--)
{
if(s[i]=='(')
{
int j=dp[i+]+i+; if(s[j]==')')
{
dp[i]=dp[i+]+;
if(j<n-) dp[i]+=dp[j+];
}
else
{
dp[i]=;
} if(dp[i]>result)
{
result=dp[i];
}
}
else
{
dp[i]=;
}
}
delete [] dp;
return result;
}
};

【leetcode】Longest Valid Parentheses的更多相关文章

  1. 【leetcode】 Longest Valid Parentheses (hard)★

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  2. 【LeetCode】20. Valid Parentheses 有效的括号

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:有效,括号,括号匹配,栈,题解,leetcode, 力扣 ...

  3. 【LeetCode】020. Valid Parentheses

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  4. 【LeetCode】20. Valid Parentheses

    题目:

  5. [LeetCode] 032. Longest Valid Parentheses (Hard) (C++)

    指数:[LeetCode] Leetcode 指标解释 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 032. Lon ...

  6. 【LeetCode】Longest Word in Dictionary through Deleting 解题报告

    [LeetCode]Longest Word in Dictionary through Deleting 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode. ...

  7. 【LeetCode】36. Valid Sudoku 解题报告(Python)

    [LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...

  8. 【LeetCode】593. Valid Square 解题报告(Python)

    [LeetCode]593. Valid Square 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...

  9. 【LeetCode】678. Valid Parenthesis String 解题报告(Python)

    [LeetCode]678. Valid Parenthesis String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人 ...

随机推荐

  1. DNS域传送漏洞利用

    DNS区域传送(DNS zone transfer)指的是一台备用服务器使用来自主服务器的数据刷新自己的域(zone)数据库.这为运行中的DNS服务提供了一定的冗余度,其目的是为了防止主的域名服务器因 ...

  2. OC-block

    #import <Foundation/Foundation.h> /* block要掌握的东西 1> 如何定义block变量 int (^sumBlock)(int, int); ...

  3. Main函数 & Autoreleasepool

    如同任何基于C的应用程序,程序启动的主入口点为iOS应用程序的main函数.在iOS应用程序,main函数的作用是很少的.它的主要工作是控制UIKit framework.因此,你在Xcode中创建任 ...

  4. C语言内存管理(转)

    伟大的Bill Gates 曾经失言: 640K ought to be enough for everybody — Bill Gates 1981 程序员们经常编写内存管理程序,往往提心吊胆.如果 ...

  5. CF469D Two Set (并查集)

    Codeforces Round #268 (Div. 2)D Codeforces Round #268 (Div. 1)B CF468B D. Two Sets time limit per te ...

  6. php怎么获取mac地址?

    如何用php获取mac地址呢?大家知道mac地址是电脑在全球范围的唯一标识,所以这个就非常实用,比如说要做一个投票功能,那mac地址是必不可少 的,如果单纯的靠ip地址来判断这个肯定是不准确的,水分太 ...

  7. 微信获取nickname中存在Emoji导致保存为空问题的解决

    微信开发时候,有些用户使用Emoji表情作为用户昵称,导致数据库保存昵称时候不识别导致昵称为空,现在提出以下解决方案: /** +----------------------------------- ...

  8. [译]Node.js Best Practices - Part 2

    原文: https://blog.risingstack.com/node-js-best-practices-part-2/ 统一风格 在大团队开发JS应用, 创建一个风格指南是很有必要的. 推荐看 ...

  9. WCF--提示:"未找到终结点。"

    刚开始调用WCF的时候一直报错... ““System.ServiceModel.EndpointNotFoundException”类型的异常在 mscorlib.dll 中发生,但未在用户代码中进 ...

  10. linux 分区问题

    一.最简单的分区 仅分出根目录(/)和最简单的内存置换空间(swap)即可 以虚拟机的分区(20G)为例: 一般给/分15G来安装linux系统,512M给swap,另外的4G可以留下备用 二.复杂一 ...