Longest Valid Parentheses - LeetCode
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.
思路:DP。
dp[i]记录最后一位是s[i]的最长合法串的长度。
很明显,当s[i] = '('时dp[i] = 0。
当s[i] = ')' 时,如果i不为0的话(为0肯定dp[i] = 0),则看一下dp[i - 1]记录的最长串的值。
而i - 1 - dp[i - 1]就是最后一位是s[i - 1]的最长合法串的前面一个字符的位置。
当dp[i - 1]等于0时,即没有合法串,该值正好是i - 1本身,而大于0时,则正好是合法串前面一位的下标。
如果s[i - 1 - dp[i - 1]]是'('的话,就正好能和当前i位置的')'匹配,从而合法串长度等于dp[i - 1] + 2。
但要注意一下,加入有如下情况,"()(())",当我们到了最后一位时,按照这个方法计算出的dp[i]=4,实际上应该为6,因为这样做忽略了前面还可能有能连起来的合法串。因此这里我们还要判断下i - 2 - dp[i - 1]是否大于等于0,如果是的话,则dp[i] = dp[i - 1] + 2 + dp[i - 2 - dp[i - 1]]。
class Solution {
public:
int longestValidParentheses(string s) {
int n = s.size(), res = ;
vector<int> dp(n, );
for (int i = ; i < n; i++)
{
if (s[i] == '(')
dp[i] = ;
else if (i - dp[i - ] - >=
&& s[i - dp[i - ] - ] == '(')
dp[i] = dp[i - ] + + (i - dp[i - ] - >= ?
dp[i - dp[i - ] - ] : );
res = max(res, dp[i]);
}
return res;
}
};
Longest Valid Parentheses - LeetCode的更多相关文章
- Longest Valid Parentheses leetcode java
题目: Given a string containing just the characters '(' and ')', find the length of the longest valid ...
- [LeetCode] Longest Valid Parentheses 最长有效括号
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- Java for LeetCode 032 Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- [LeetCode] Longest Valid Parentheses 解题思路
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- [Leetcode][Python]32: Longest Valid Parentheses
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 32: Longest Valid Parentheseshttps://oj ...
- [LeetCode] 032. Longest Valid Parentheses (Hard) (C++)
指数:[LeetCode] Leetcode 指标解释 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 032. Lon ...
- 【一天一道LeetCode】#32. Longest Valid Parentheses
一天一道LeetCode系列 (一)题目 Given a string containing just the characters '(' and ')', find the length of t ...
- LeetCode 笔记系列八 Longest Valid Parentheses [lich你又想多了]
题目:Given a string containing just the characters '(' and ')', find the length of the longest valid ( ...
- [Leetcode] longest valid parentheses 最长的有效括号
Given a string containing just the characters'('and')', find the length of the longest valid (well-f ...
随机推荐
- 3224: Tyvj 1728 普通平衡树(finger tree)
3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 19122 Solved: 8359[Submit][St ...
- ios开发学习笔记040-autolayout 第三方框架Masonry
不管是是界面创建约束还是代码创建约束,苹果官方提供的方式都比较繁琐.所以出现了第三方框架. Masonry 在github地址如下: https://github.com/SnapKit/Masonr ...
- Android 使用intent传递返回值:startActivityForResult()与onActivityResult()与setResult()参数分析,activity带参数的返回
在一个父Activity通过intent跳转至多个不同子Activity上去,当子模块的代码执行完毕后再次返回父页面,将子activity中得到的数据显示在主界面/完成的数据交给父Activity处理 ...
- Python-S9-Day101 Vue-cli
01 昨天内容回顾 02 音乐播放器计算属性方法和组件创建 03 Vue-cli项目生成 04 模板中组件的使用 01 昨天内容回顾 1.1 {{xxx}}模板语法,插值,简单的运算: 1.2 指令系 ...
- Form 组件动态绑定数据
1.Form 组件的作用: a.对用户提交的数据进行验证(form表单/ajax) b.保留用户上次输入的信息 c.可以生成html标签(input表单类的标签) 2..由于form组件中每个字段都是 ...
- [python][django学习篇][12]继续设计博客首页,点击博客标题能显示文章的详情
回顾一下开发流程:配置url, 编写视图函数,编写对应模板 配置URL 首页视图匹配的 URL 去掉域名后,是一个空的字符串.每篇文章的详情有着不同的 URL,因此可以设计文章详情页面URl:< ...
- 模板与c++11--右值引用
函数参数传递 struct huge_data{ char *content; unsigned sz; huge_data():content(),sz(){ cout<<this< ...
- SSM框架 springMVC对静态资源访问的处理
https://my.oschina.net/hnqingping1255/blog/415575 错误信息 [org.springframework.web.servlet.PageNotFound ...
- 【转】log4js在PM2的cluster模式下大坑
请直接查看原文:https://blog.yourtion.com/fix-log4js-with-pm2-not-work.html 之前一直使用 debug 还有 console.log 去打日志 ...
- 【转】UGUI VS NGUI
原文:http://gad.qq.com/college/articledetail/7191053 注[1]:该比较是基于15年-16年期间使用NGUI(3.8.0版本)与UGUI(4.6.9版本) ...