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 ...
随机推荐
- 学习笔记之pandas
Python Data Analysis Library — pandas: Python Data Analysis Library https://pandas.pydata.org/ panda ...
- grep简单用法
grep 常用参数: -c: 打印符合要求的行数 -i :忽略大小写 -n:输出行和行号 -v:打印不符合要求的行,即反选 -A:后跟数字(有无空格都可以),例如-A2 表示打印筛选行及前2行 -B: ...
- 父级元素position:absolute,子节点也是absolute
当父级属性position是absolute,子节点也是absolute的时候,子节点是相对父级absolute的,举个例子 <div class="headManage"& ...
- 阿里云OSS图片云存储测试上传
在开发DEMO之前首先要确定 你开发OSS服务并获取了 accessKeyId和accessKeySecret final String key = MD5.Md5(DateFormat.format ...
- IIS 反向代理设置
http://blog.csdn.net/yuanguozhengjust/article/details/23576033
- python中转义符&str格式化
转义字符: 1.将有意义的字符变的无意义 2.将无意义的字符变的有意义 语法: \ + 某个字符 \n, \r\n : 代表换行 \t : 代表一个缩进, (水平制表符) \r : ...
- 第32课 Linux内核链表剖析
1. Linux内核链表的位置及依赖 (1)位置:{linux-2.6.39}\\include\linux\list.h (2)依赖 ①#include<linux\types.h> ② ...
- Linux交换空间(swap space)
每次安装Linux的时候,都会要求配置交换分区,那么这个分区是干嘛的呢?不设置这个分区有什么后果?如果一定要设置,设置多大比较合适?本篇将试图回答这些问题并尽量覆盖所有swap相关的知识. 下面的所有 ...
- CF603EPastoral Oddities
/* LCT管子题(说的就是你 水管局长) 首先能得到一个结论, 那就是当且仅当所有联通块都是偶数时存在构造方案 LCT动态加边, 维护最小生成联通块, 用set维护可以删除的边, 假如现在删除后不影 ...
- Python-第三方模块requests快速入手
首先确认一下 Requests 已经安装 Requests 是最新的版本 如果没有安装requests,请按照下面的方式安装 安装requests window和Linux环境下都可以输入 $ pip ...