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 ...
随机推荐
- grafana 指标视图嵌入到其他html网页
我们开发了一套管理平台用来监控整个系统环境的运行情况,但是在指标信息这块不想重新开发,而想直接拿grafana来用,刚开始的时候我们的管理平台和grafana是完全独立的,只能从我们平台跳转到graf ...
- MySQL binlog 企业案例升级版
需求:1.创建一个数据库 oldboy2.在oldboy下创建一张表t13.插入5行任意数据4.全备5.插入两行数据,任意修改3行数据,删除1行数据6.删除所有数据7.再t1中又插入5行新数据,修改3 ...
- Linux镜像源
1. 国内镜像源 (1.)备份原有镜像源文件 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup ...
- AD中组的概念
- OpenJudge 兔子与樱花
[题解] 求任意两点间的最短路径.此题数据量较小,用Floyd算法,时间复杂度为O(n^3). 参考https://blog.csdn.net/qq_34594236/article/details/ ...
- 1-自己动手编写ArrayList集合
学习集合的原理,这是一个很简单的代码,没有索引下标越界判断,异常处理等,不够健壮,后面会继续更新完善代码..... 只是入门学习一下 package my; /*** * 自己动手编写一个ArrayL ...
- MySQL多表查询,pymysql模块。
一 多表查询: 首先什么是多表查询: 我们在实际工作中,不可能把数据都存入一个表中,那么又需要这些表之间有一定的关联,因为表与表之间的数据是相关联的,所以就要用到我们的外键将多表连接到一起,那么我们更 ...
- nodejs开篇基础<①>
1.安装相关 //安装brew Homebrew ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/insta ...
- python中使用tabula爬取pdf数据并导出表格
Tabula是专门用来提取PDF表格数据的,同时支持PDF导出CSV.Excel格式. 首先安装tabula-py: tabula-py依赖库包括Java.pandas.numpy所以需要保证运行环境 ...
- 学习笔记: js插件 —— SuperSlide 2 (轮播图插件,PC用)
SuperSlide 2 轮播图插件,较老.但还好用. 适用于PC,是绑定到jquery上的方法: $.slide(); 如果在实际中找不到.slide方法,请检查jquery等.js文件的引入次序 ...