题目:

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. HDU ACM 1051/ POJ 1065 Wooden Sticks

    Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  2. U盘分区信息清除

    diskpart select disk 1 clean 清除选中(优U)盘的所有信息;

  3. index structure

    1. wordlist 0) 0, 1byte 1) token-id(delta), 8byte 2) doclist-offset(delta), 8byte 3) doc_count, 4byt ...

  4. EntityFramework简单例子

    @(编程) 这个例子是用vs2013连接mysql数据库. 1. NuGet安装EF和mysql 略 2. 对象 namespace EFDemo { class Student { public s ...

  5. 【Android框架进阶〖0〗】ThinkAndroid注解机制

    由于项目需要,开始研究ThinkAndroid. 个人认为该框架的注解机制十分新颖,所以先研究这个,顺便学习下 Java 的annotation. 粗略的看了看,该机制在BaseActivity中初始 ...

  6. dbcp 是什么

    Many Apache projects support interaction with a relational database. Creating a new connection for e ...

  7. Spring JTA应用JOTM & Atomikos III Atomikos

    前面简单介绍了JOTM如何在Spring中配置,并如何使用它的JTA事务,本节将介绍Atomikos如何与Spring集成,并使用它的JTA事务. Atomikos,是一个基于Java的开源事务管理器 ...

  8. openfire搭建IM

    1.openfire 安装配置 在官网上下载,下载最新版本. 在数据库配置方面: 驱动:net.sourceforge.jtds.jdbc.Driver数据库连接:jdbc:jtds:sqlserve ...

  9. EasyMock使用手记

    from:http://www.blogjava.net/supercrsky/articles/162766.html Mock 对象能够模拟领域对象的部分行为,并且能够检验运行结果是否和预期的一致 ...

  10. Junit 测试断言说明

    Assert.assertEquals("发生错误时报告消息","预期值","生产值"); Assert.assertEquals(&quo ...