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解的更多相关文章

  1. leetcode解题报告 32. Longest Valid Parentheses 用stack的解法

    第一道被我AC的hard题!菜鸡难免激动一下,不要鄙视.. Given a string containing just the characters '(' and ')', find the le ...

  2. [Leetcode][Python]32: Longest Valid Parentheses

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 32: Longest Valid Parentheseshttps://oj ...

  3. leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、

    20. Valid Parentheses 错误解法: "[])"就会报错,没考虑到出现')'.']'.'}'时,stack为空的情况,这种情况也无法匹配 class Soluti ...

  4. 刷题32. Longest Valid Parentheses

    一.题目说明 题目是32. Longest Valid Parentheses,求最大匹配的括号长度.题目的难度是Hard 二.我的做题方法 简单理解了一下,用栈就可以实现.实际上是我考虑简单了,经过 ...

  5. [LeetCode] 32. Longest Valid Parentheses 最长有效括号

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

  6. 【一天一道LeetCode】#32. Longest Valid Parentheses

    一天一道LeetCode系列 (一)题目 Given a string containing just the characters '(' and ')', find the length of t ...

  7. Java [leetcode 32]Longest Valid Parentheses

    题目描述: Given a string containing just the characters '(' and ')', find the length of the longest vali ...

  8. 【LeetCode】32. Longest Valid Parentheses

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

  9. leetcode 32. Longest Valid Parentheses

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

随机推荐

  1. event.currentTarget指向事件所绑定的元素,而event.target始终指向事件发生时的元素

    event.currentTarget指向事件所绑定的元素,而event.target始终指向事件发生时的元素

  2. jquery tr:even,tr:eq(),tr:nth-child()区别

    jquery里面是不是搞不清楚,tr的选择器? $("tr:even"),$("tr:eq(2)"),$("tr:eq(3)"),$(&qu ...

  3. 梯度下降(Gradient Descent)相关概念

    梯度,直观理解: 梯度: 运算的对像是纯量,运算出来的结果会是向量在一个标量场中, 梯度的计算结果会是"在每个位置都算出一个向量,而这个向量的方向会是在任何一点上从其周围(极接近的周围,学过 ...

  4. Elasticsearch分布式机制探究

    Elasticsearch是一套分布式的系统,分布式是为了应对大数据量隐藏了复杂的分布式机制 分片机制 shard = hash(routing) % number_of_primary_shards ...

  5. TableStore:单行操作

    说明: 首先需要添加TableStore的依赖 <dependency> <groupId>com.aliyun.openservices</groupId> &l ...

  6. python学习之----BeautifulSoup示例二

    网络爬虫可以通过class 属性的值,轻松地区分出两种不同的标签.例如,它们可以用 BeautifulSoup 抓取网页上所有的红色文字,而绿色文字一个都不抓.因为CSS 通过属性准 确地呈现网站的样 ...

  7. svn 提交数据

    linux

  8. 企业项目构建学习(一)maven

    <mirrors> <mirror> <id>alimaven</id> <name>aliyun maven</name> & ...

  9. mongodb的管理员和安全认证

    超级管理员 为了更安全的访问mongodb,需要访问者提供用户名和密码,于是需要在mongodb中创建用户 采用了角色-用户-数据库的安全管理方式 常用系统角色如下: root:只在admin数据库中 ...

  10. uva-704-暴力枚举-双向bfs

    题意:俩个转盘,24个数字,每个转盘都可以顺时针,逆时针旋转.终点固定. 问:给定一个起点,能不能在16步内转到终点 解法:双向bfs,终点走8步,起点走8步,判断从起点生成的状态是否在终点里面,如果 ...