LeetCode32 Longest Valid Parentheses
题目:
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的更多相关文章
- [Swift]LeetCode32. 最长有效括号 | 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 ...
- 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 ...
- 【leetcode】Longest Valid Parentheses
Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...
- 【leetcode】 Longest Valid Parentheses (hard)★
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- Longest Valid Parentheses 每每一看到自己的这段没通过的辛酸代码
Longest Valid Parentheses My Submissions Question Solution Total Accepted: 47520 Total Submissions: ...
- [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 ...
随机推荐
- 第三百三十四天 how can I 坚持
I give up my dream that day,else,I coming on,the day my heart is die…… 那天,梦已碎,那天,心已死. 晚上看了个电影<奔爱& ...
- 第二百零一天 how can I坚持
sql要学的东西还很多,很简单的一个sql都不会写,还得请教别人,哎. 八千代.铜钱草,小叶元宝,绿萝.还有我的鱼,还有罗娜. 今天试用了一下三星,系统优化就是不行啊,掉电太快,想搞个小米5,还想买个 ...
- 转】MyEclipse使用总结——MyEclipse中配置WebLogic12c服务器
原博文出自于:http://www.cnblogs.com/xdp-gacl/p/4142495.html 感谢! MyEclipse中配置WebLogic12c服务器的步骤如下: [Window]→ ...
- WebBrowser的各种使用方法(未完待续)(XE8+WIN7)
相关资料: 占时想不起来了,有时间我补上吧. 程序下载: http://download.csdn.net/detail/zhujianqiangqq/9666390 实例代码: unit Unit1 ...
- HDU 2034 人见人爱A-B 分类: ACM 2015-06-23 23:42 9人阅读 评论(0) 收藏
人见人爱A-B Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Su ...
- C#取得当前目录 转载
/获取包含清单的已加载文件的路径或 UNC 位置. public static string sApplicationPath = Assembly.GetExecutingAssem ...
- android ListView进阶
ListView 1.在android 开发中很多时候都要用到ListView的这个控件的,但用这个控件的时候会遇到一些问题,如在ListView中有Button按钮,就需要将按钮的监听事件给分离出来 ...
- js获取当前页面的url信息方法
例如网址:http://localhost:12085/My/OrderM.aspx 设置或获取对象指定的文件名或路径. alert(window.location.pathname) 输出结果:/M ...
- iOS 在任意界面 Dismiss Keyboard
最近由于项目需要,有些时候我们需要在任意时刻dismiss掉键盘. 很自然的我们会想到键盘通知 UIKeyboardDidShowNotification和UIKeyboardDidHideNotif ...
- 在ASP.NET MVC中使用MySQL【并使用membership】
大多数情况下我们使用.NET或ASP.NET(包括MVC)程序时,我们会同时选择SQL Server 或者SQL Express (其他微软产品)做数据库.但是今天使用MVC已经完全没 ...