【leetcode】Longest Valid Parentheses
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.
class Solution {
public:
int longestValidParentheses(string s) {
int n=s.length();
if(n==) return ;
int *dp=new int[n];
dp[n-]=;
int result=;
for(int i=n-;i>=;i--)
{
if(s[i]=='(')
{
int j=dp[i+]+i+;
if(s[j]==')')
{
dp[i]=dp[i+]+;
if(j<n-) dp[i]+=dp[j+];
}
else
{
dp[i]=;
}
if(dp[i]>result)
{
result=dp[i];
}
}
else
{
dp[i]=;
}
}
delete [] dp;
return result;
}
};
【leetcode】Longest Valid Parentheses的更多相关文章
- 【leetcode】 Longest Valid Parentheses (hard)★
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- 【LeetCode】20. Valid Parentheses 有效的括号
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:有效,括号,括号匹配,栈,题解,leetcode, 力扣 ...
- 【LeetCode】020. Valid Parentheses
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- 【LeetCode】20. Valid Parentheses
题目:
- [LeetCode] 032. Longest Valid Parentheses (Hard) (C++)
指数:[LeetCode] Leetcode 指标解释 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 032. Lon ...
- 【LeetCode】Longest Word in Dictionary through Deleting 解题报告
[LeetCode]Longest Word in Dictionary through Deleting 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode. ...
- 【LeetCode】36. Valid Sudoku 解题报告(Python)
[LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...
- 【LeetCode】593. Valid Square 解题报告(Python)
[LeetCode]593. Valid Square 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...
- 【LeetCode】678. Valid Parenthesis String 解题报告(Python)
[LeetCode]678. Valid Parenthesis String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人 ...
随机推荐
- input 框 宽度100%时 padding 超出问题解决
如下图: 让input 宽度100%, 加边框并有左填充,这里如果用 padding-left: 的话,input 边框会超出100%的范围 后来发现 text-indet: XXpx; 即可实现即 ...
- css样式中遇到!important
链接: http://zhidao.baidu.com/link?url=XyYHS2l-bFkzEgMBWfbQYuEV4vmGz8kOhj-jDL_HBqu0KnVIBQJKvw5OOO-bVjv ...
- Orchard源码分析(6):Shell相关
概述在Orchard中,提出子站点(Tenant)的概念,目的是为了增加站点密度,即一个应用程序域可以有多个子站点. Shell是子站点(Tenant)级的单例,换句话说Shell代表了子站点.对比来 ...
- Java数据库连接池封装与用法
Java数据库连接池封装与用法 修改于抄袭版本,那货写的有点BUG,两个类,一个用法 ConnectionPool类: package com.vl.sql; import java.sql.Conn ...
- 用java实现zip压缩
本来是写到spaces live上的,可是代码的显示效果确实不怎么好看.在javaeye上试了试代码显示的顺眼多了. 今天写了个用java压缩的功能,可以实现对文件和目录的压缩. 由于java.uti ...
- [Ljava.lang.String和java.lang.String区别
在做项目时报了一个got class [Ljava.lang.String的提示,当时看到[Ljava.lang.String这个时,感觉有点怪怪的,第一次遇到这种情况.最后在网上查了下才明白.是数组 ...
- [C#]System.Timers.Timer
摘要 在.Net中有几种定时器,最喜欢用的是System.Timers命名空间下的定时器,使用起来比较简单,作为定时任务,有Quartz.net,但有时候,一个非常简单的任务,不想引入这个定时任务框架 ...
- js 的强制 类型 转换cast, 伪对象?
拼音输入法简单快捷, 但是重码多, 所以要看清了再选择, 不然会影响输入正确性和 心情的! js的类型 隐式 转换? 是指, 根据 表达式的操作符号 如if(), + , && , j ...
- 如何运行python
如何运行Python程序 不少新手朋友遇到的第一个问题,不是Python的数据结构,不是Python的类库使用,更不是第三方模块.框架的应用,而是简单的“如何运行”!Python可用以下三种方式运行, ...
- 关于IE7 默认有边框的解决方案
这个问题出现在IE7中,因为body有默认的border.这个原因是由于声明引起的. 加了这个头就可以了 * {border:0;} 以上的 CSS 在 XHTML 下是无效果的,将 DOCTYPE ...