题目:

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. (Hard)

分析:

题意是找到最长的合法括号字串。

看着就很想动态规划的题目,但是开始考虑的是dp[i][j]记录i到j是否是合法串。但是递推关系没法找。

想到应该是符合单序列动态规划的情况,用dp[i]表示以i结尾的最长合法括号串的长度。

递推关系如下:

如果 s[i] == '('  则 dp[i] = 0;

如果 s[i] == ')'  则

  如果 s[i - 1] == '(' 则 dp[i] = dp[i - 2] + 2;

  如果 s[i - 1] == ')' 则需要记录 j = dp[i - 1],并判断 s[i - 1 - j] 也就是从后往前数第一个不符合的字符的情况。

        如果s[i - 1- j] == '('   则 dp[i] = dp[i - j -2] + dp[i - 1] + 2;  // i-j-2位置向前合法的,加上i-1位置向前合法的,加上s[i-j-1]和s[i]配对的2;

        如果s[i - 1 - j]不存在或者为 ')' 则 s[i] = 0;

求出不等于0的dp[i]时更新result。

把上述递推关系实现代码如下:

 class Solution {
public:
int longestValidParentheses(string s) {
if (s.size() == || s.size() == ) {
return ;
}
int result = ;
int dp[s.size()] = {};
if (s[] == ')' && s[] == '(') {
dp[] = ;
result = ;
}
for (int i = ; i < s.size(); ++i) {
if (s[i] == '(') {
dp[i] = ;
}
if (s[i] == ')') {
if (s[i - ] == '(') {
dp[i] = dp[i - ] + ;
result = max(result,dp[i]);
}
if (s[i - ] == ')') {
int j = dp[i - ];
if (i - j - >= && s[i - j - ] == '(') {
dp[i] = dp[i - ] + dp[i - j - ] + ;
result = max(result,dp[i]);
}
else {
dp[i] = ;
}
}
}
}
return result;
}
};

LeetCode32 Longest Valid Parentheses的更多相关文章

  1. [Swift]LeetCode32. 最长有效括号 | Longest Valid Parentheses

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

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

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

  3. Longest Valid Parentheses

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

  4. leetcode 32. Longest Valid Parentheses

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

  5. 【leetcode】Longest Valid Parentheses

    Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...

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

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

  7. Longest Valid Parentheses 每每一看到自己的这段没通过的辛酸代码

    Longest Valid Parentheses My Submissions Question Solution  Total Accepted: 47520 Total Submissions: ...

  8. [LeetCode] Longest Valid Parentheses 动态规划

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

  9. Java for LeetCode 032 Longest Valid Parentheses

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

随机推荐

  1. 第三百三十二天 how can I 坚持

    今天一大早,住的这就施工了,被吵醒了.. 下午去了趟小米之家,小米5还行,黑科技不黑,哈哈. 小米5黑科技不太黑,就知道造词,整体感觉还行,就是感觉屏幕有点长,小米之家人倒是不少,还有老太太去小米之家 ...

  2. 第三百二十三天 how can I 坚持

    人在最绝望的时候会干啥,<进击的巨人>. 可以绝望,但一定要相信还有希望. 今天去看了<美人鱼>,确实挺好吧. 把愤怒归结于无能,其实是大错特错,愤怒是人的情绪的发泄,是人就有 ...

  3. vim编辑十六进制文件

    首先用二进制方式打开 vim file -b 之后输入 :%!xxd 还原为二进制文件 :%!xxd -r

  4. Mysql SQL优化&执行计划

    SQL优化准则 禁用select * 使用select count(*) 统计行数 尽量少运算 尽量避免全表扫描,如果可以,在过滤列建立索引 尽量避免在where子句对字段进行null判断 尽量避免在 ...

  5. FATFS文件系统

    STM32移植文件系统,操作SD卡,对SD卡进行读写 FATFS文件系统与底层介质的驱动分离开来,对底层介质的操作都要交给用户去实现,它仅仅是提供了一个函数接口而已,函数为空,要用户添加代码.然后 F ...

  6. CISCO ASA 防火墙 IOS恢复与升级

    在IOS被误清除时的处理办法: 1.从tftp上的ios启动防火墙 防火墙启动后 ,按“ESC”键进入监控模式 rommon #2> ADDRESS=192.168.1.116 rommon # ...

  7. 在sql中使用了 hashbytes 函数

    在做项目的时候,在sql中使用了hashbytes函数,运用md5算法去加密一密码.代码如下 DECLARE @psw nvarchar(20) SET @psw = 'admin' SELECT h ...

  8. MySQL主从复制的原理及配置

    [http://www.jb51.net/article/50053.htm]   MySQL 数据库的高可用性架构:         集群,读写分离,主备.而后面两种都是通过复制来实现的.下面将简单 ...

  9. C#以及Oracle中的上取整、下取整方法

    1.C#中: 上取整——Math.Ceiling(Double),即返回大于或等于指定双精度浮点数的最大整数(也可称为取天板值): eg:  Math.Ceiling(1.01)=2;      Ma ...

  10. cocos2d-x 使用Lua

    转自:http://www.benmutou.com/blog/archives/49 1. Lua的堆栈和全局表 我们来简单解释一下Lua的堆栈和全局表,堆栈大家应该会比较熟悉,它主要是用来让C++ ...