题目:

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. 验证dictionary重复键

    if (dict.ContainsKey("sadsa")) { }

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

    家里断网了,忘交网费了,连的手机网络,也挺好,吃完饭就可以睡觉了. 不知道怎的,昨天和家人聊天,一提对象的事就很容易着急生气,然后就会后悔..哎,这脾气得改. 确实不知道自己的另一半是啥样,想象不出来 ...

  3. MYSQL数据库性能调优之五:解决慢查询--存储引擎与数据类型

    3.数据类型的影响 4.存储引擎的影响 看你的mysql现在已提供什么存储引擎:mysql> show engines; 看你的mysql当前默认的存储引擎:mysql> show var ...

  4. UVALive 5881 Unique Encryption Keys (DP)

    Unique Encryption Keys 题目链接: http://acm.hust.edu.cn/vjudge/problem/26633 Description http://7xjob4.c ...

  5. 遇见了这个问题:App.config提示错误“配置系统未能初始化”

    解决办法查找之后居然是这样的,受教了,记录一下 解决: "如果配置文件中包含 configSections 元素,则 configSections 元素必须是 configuration 元 ...

  6. ACM之数学题

    数学题,始终记得,第一次被带飞师大校赛以及省赛,毫无例外的在数学题上卡死....因此,现在开始,有意识的保留遇见的数学题...(下列知识点按遇见先后顺序排列: 1欧拉公式 欧拉公式的用处是,找出小于N ...

  7. js关闭当前页面(窗口)的几种方式总结

    1. 不带任何提示关闭窗口的js代码 <a href="javascript:window.opener=null;window.open('','_self');window.clo ...

  8. POJ3321 Apple Tree (树状数组)

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16180   Accepted: 4836 Descr ...

  9. Top 7 Myths about HTTPS

    Myth #7 – HTTPS Never Caches People often claim that HTTPS content is never cached by the browser; p ...

  10. 转载 在 Linux 虚拟机中手动安装或升级 VMware Tools

    http://pubs.vmware.com/workstation-12/index.jsp?lang=zh_CN&topic=/com.vmware.ws.using.doc/GUID-0 ...