【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 个人 ...
随机推荐
- ASP.NET MVC4/5 - Ajax 防止 CSRF攻击
前言 CSRF(Cross-site request forgery跨站请求伪造,也被称为“One Click Attack”或者Session Riding,通常缩写为CSRF或者XSRF,是一种对 ...
- java的集合框架最全详解
java的集合框架最全详解(图) 前言:数据结构对程序设计有着深远的影响,在面向过程的C语言中,数据库结构用struct来描述,而在面向对象的编程中,数据结构是用类来描述的,并且包含有对该数据结构操作 ...
- Linux服务器管理: RPM包
服务安装类型主要分两种: RPM安装和源码包编译安装: 1.RPM包查看: rpm -qa | grep 包名 用chkconfig --list 查看服务自启动命令 只对RPM包安装的服务生效 ...
- hdu4965 Fast Matrix Calculation (矩阵快速幂 结合律
http://acm.hdu.edu.cn/showproblem.php?pid=4965 2014 Multi-University Training Contest 9 1006 Fast Ma ...
- 阿里云Centos配置iptables防火墙
虽说阿里云推出了云盾服务,但是自己再加一层防火墙总归是更安全些,下面是我在阿里云vps上配置防火墙的过程,目前只配置INPUT.OUTPUT和FORWORD都是ACCEPT的规则 一.检查iptabl ...
- 【C语言入门教程】4.8 指针数组
指针数组是一种特殊的数组,这类数组存放的全部是同一数据类型的内存地址.指针数组的定义形式为: 数据类型 *数组名[长度]; 例如: const char *c[4] = { "China&q ...
- PPPoE名词解释
PPPoE拔号的发现阶段(Discovery): PPPoE的发现阶段一共分为4步. 分别是: PADI(PPPoE Active Discovery Initiation) PADO(PPPoE A ...
- 利用Jquery的load函数实现页面的动态加载
利用Jquery的load函数实现页面的动态加载 js的强大功能相信大家都知晓,今天通过jquery的库函数load可以更加方便的实现页面的动态刷新,经过几天的研究与探索,终于有所成效!吾心甚蔚! ...
- PHP Socket实现websocket(二)Socket函数
PHP socket函数是调用系统的的Socket函数,可以参考C语言的socket函数. Socket函数:http://php.net/manual/en/book.sockets.php 服务器 ...
- plsql 建立oracle作业
--.plsql中学习job --学习job --建表 create table test_job(para_date date); commit; insert into test_job valu ...