leetcode解题报告 32. Longest Valid Parentheses 动态规划DP解
dp[i]表示以s[i]结尾的完全匹配的最大字符串的长度。
dp[] = ;
if( s[i] == '(' )
dp[i] = ;
开始递推 s[i] = ')' 的情况
先想到了两种情况:
1、s[i-1] = '(' 相邻匹配

这种情况下,dp [i] = dp[i-2] + 2。
2、s[i-1] = ')'

这种情况下,第一感觉是要看dp[i-1]的值,即 j...i-1是完全匹配的话,i相当于在外面再包一个括号。
如果s[i] 和 s[ i-1-dp[i-1] ] 匹配,dp[i] = dp[i-1] + 2。否则dp[i] = 0。
提交发现 WA。
不通过的数据是这个:

上图中的23列,本来23应该和2列的'('匹配,得到22。但是计算中,23列只得到了6。
加上一行代码AC了: dp[i] += dp[ i-1-dp[i-1] ];
如果当前完整块前面相邻了一个完整块,就把它的长度也算在内。
连蒙带猜的过了。。。有点水。希望以后赶紧提升。
int longestValidParentheses(char *s)
{
int n = strlen(s);
int i,cnt ,ans = ; if(n == && s == NULL) return ; dp = (int*)malloc(n*sizeof(int)); //dp[i]表示以s[i]结尾的匹配字符串的长度。 memset(dp,,n*sizeof(int)); for(i=;i<n;i++)
{
if( s[i]=='(' ){
dp[i] = ;
}
else
{ /* 0 1 2 3 4 5 6 i */
if( s[i-] == '(' ) /* ( ( ( ) ) ) ( ) */
{
int tmp = i-;
if(tmp >= )
{
dp[i] = dp[tmp] + ;
}
else
{
dp[i] = ;
}
}
/* 0 1 2 3 4 5 i ( ( ( ) ) ) ) 0 0 0 2 4 6
*/
else
{
int tmp = i--dp[i-];
if(tmp >= && s[tmp] == '(')
{
dp[i] = dp[i-] + ;
dp[i] += dp[i-dp[i]];
}
}
}
}
cnt = ;
ans = ;
for( i=n-; i>=; )
{
if(dp[i] > )
{
cnt += dp[i];
i -= dp[i]; if(cnt > ans) ans = cnt;
}
else
{
cnt = ;
--i;
}
}
free(dp); return ans;
}
leetcode解题报告 32. Longest Valid Parentheses 动态规划DP解的更多相关文章
- leetcode解题报告 32. Longest Valid Parentheses 用stack的解法
第一道被我AC的hard题!菜鸡难免激动一下,不要鄙视.. Given a string containing just the characters '(' and ')', find the le ...
- [Leetcode][Python]32: Longest Valid Parentheses
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 32: Longest Valid Parentheseshttps://oj ...
- leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、
20. Valid Parentheses 错误解法: "[])"就会报错,没考虑到出现')'.']'.'}'时,stack为空的情况,这种情况也无法匹配 class Soluti ...
- 刷题32. Longest Valid Parentheses
一.题目说明 题目是32. Longest Valid Parentheses,求最大匹配的括号长度.题目的难度是Hard 二.我的做题方法 简单理解了一下,用栈就可以实现.实际上是我考虑简单了,经过 ...
- [LeetCode] 32. Longest Valid Parentheses 最长有效括号
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- 【一天一道LeetCode】#32. Longest Valid Parentheses
一天一道LeetCode系列 (一)题目 Given a string containing just the characters '(' and ')', find the length of t ...
- Java [leetcode 32]Longest Valid Parentheses
题目描述: Given a string containing just the characters '(' and ')', find the length of the longest vali ...
- 【LeetCode】32. Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- leetcode 32. Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
随机推荐
- Spring4.0之四:Meta Annotation(元注解)
Spring框架自2.0开始添加注解的支持,之后的每个版本都增加了更多的注解支持.注解为依赖注入,AOP(如事务)提供了更强大和简便的方式.这也导致你要是用一个相同的注解到许多不同的类中去.这篇文章介 ...
- tools、site
notepad editer fiddler 一个http协议调试代理工具,它能够记录并检查所有你的电脑和互联网之间的http通讯,设置断点,查看所有的"进出"Fiddler的数据 ...
- [UE4]给Widget增加参数,Pre Construct和Construct的区别
使用Pre Construct事件可以在编辑器中实时显示出选择的背景图片. 如果使用的是“Construct”事件则只能在游戏运行时把图片显示出来.
- https证书的验证过程与生成方法
1.简洁的解释: 1.服务器 用RSA生成公钥和私钥2.把公钥放在证书里发送给客户端,私钥自己保存3.客户端首先向一个权威的服务器检查证书的合法性,如果证书合法,客户端产生一段随机数,这个随机数就作为 ...
- 谷歌获取chromium
转自:http://blog.sina.com.cn/s/blog_496be0db0102voit.html 先参看 http://www.chromium.org/developers/how-t ...
- javascript-时间戳
// 获取当前时间戳(以s为单位) var timestamp = Date.parse(new Date()); timestamp = timestamp / 1000; console.log( ...
- unity3d简介
一.介绍: Unity3D软件:综合开发环境,实时三维动画等类型的多媒体内容,并支持这些内容在Windows.iOS.Android等多种平台的发布. Mono:脚本编程基于Mono技术,可使用Jav ...
- (转).Net高级进阶,在复杂的业务逻辑下,如何以最简练的代码,最直观的编写事务代码?
原文地址:http://www.cnblogs.com/1996V/p/7798111.html 示例一和示例二,主要是来讲解 TransactionScope 是什么,为什么要用Transactio ...
- Html5——视频标签使用
video标签: 上面的例子使用一个 Ogg 文件,适用于Firefox.Opera 以及 Chrome 浏览器.要确保适用于 Safari 浏览器,视频文件必须是 MPEG4 类型.video 元素 ...
- Java - 25 Java 包(package)
Java 包(package) 为了更好地组织类,Java提供了包机制,用于区别类名的命名空间. 包的作用 1 把功能相似或相关的类或接口组织在同一个包中,方便类的查找和使用. 2 如同文件夹一样,包 ...