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.

思路:自己按照早上Scramble String (hard)★ 的动态规划方法试着做了一遍,‘(’表示为1, ‘)’表示为-1,用dp[start]记录不同长度情况下以start开始的括号对应的数字之和。如果为0则表示有效,更新最大长度。 结果超时了,说明我这个O(n2)的方法不行。

    int longestValidParentheses(string s) {
string scpy = s;
int ans = ;
while(!scpy.empty() && scpy[] == ')') //跳过位于前面的 ) 因为肯定无法配对
{
scpy.erase(scpy.begin());
}
if(scpy.empty())
return ans; vector<int> dp; //dp[start]
vector<int> dp1;
for(int start = ; start <= scpy.length() - ; start++)
{
dp.push_back((scpy[start] == '(') ? : -);
dp1.push_back(dp.back());
}
for(int len = ; len <= scpy.length(); len++)
{
for(int start = ; start <=scpy.length() - len; start++)
{
dp[start] = dp[start] + dp1[start + len - ];
if(dp[start] == && len > ans)
{
ans = len;
}
}
}
return ans;
}

大神可以通过而且简洁的O(n)方法

用longest[i]存储以 i 结尾时最大有效长度(必须包括第 i 个字符)

如果s[i] = '('   longest[i] = 0

else s[i] = ')'

If s[i-1] is '(', longest[i] = longest[i-2] + 2

Else if s[i-1] is ')' and s[i-longest[i-1]-1] == '(',  longest[i] = longest[i-1] + 2 + longest[i-longest[i-1]-2]

the condition "s[i-1] == '('" since "s[i-longest[i-1]-1] == '('" actually concludes this case. We could just use "if (i-1>=0 && i-longest[i-1]-1 >=0 && s[i-longest[i-1]-1] == '(')"

int longestValidParentheses(string s) {
if(s.length() <= ) return ;
int curMax = ;
vector<int> longest(s.size(),);
for(int i=; i < s.length(); i++){
if(s[i] == ')' && i-longest[i-]- >= && s[i-longest[i-]-] == '('){
longest[i] = longest[i-] + + ((i-longest[i-]- >= )?longest[i-longest[i-]-]:);
curMax = max(longest[i],curMax);
}
}
return curMax;
}

【leetcode】 Longest Valid Parentheses (hard)★的更多相关文章

  1. 【leetcode】Longest Valid Parentheses

    Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...

  2. 【LeetCode】20. Valid Parentheses 有效的括号

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:有效,括号,括号匹配,栈,题解,leetcode, 力扣 ...

  3. 【LeetCode】020. Valid Parentheses

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  4. 【LeetCode】20. Valid Parentheses

    题目:

  5. [LeetCode] 032. Longest Valid Parentheses (Hard) (C++)

    指数:[LeetCode] Leetcode 指标解释 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 032. Lon ...

  6. 【LeetCode】Longest Word in Dictionary through Deleting 解题报告

    [LeetCode]Longest Word in Dictionary through Deleting 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode. ...

  7. 【LeetCode】36. Valid Sudoku 解题报告(Python)

    [LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...

  8. 【LeetCode】593. Valid Square 解题报告(Python)

    [LeetCode]593. Valid Square 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...

  9. 【LeetCode】678. Valid Parenthesis String 解题报告(Python)

    [LeetCode]678. Valid Parenthesis String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人 ...

随机推荐

  1. 第二章平稳时间序列模型——ACF和PACF和样本ACF/PACF

    自相关函数/自相关曲线ACF   AR(1)模型的ACF: 模型为: 当其满足平稳的必要条件|a1|<1时(所以说,自相关系数是在平稳条件下求得的):          y(t)和y(t-s)的 ...

  2. VC++6.0MFC运行的简单流程

    背景: 由于下位机做的一些东西,总是需要通过上位机来验证,以及为了直观给客户展示下位机的功能,所以时常需要编写一些简单的APP.今天就以VC++6.0为例,简单的记录下该如何快速的创建一个APP. 正 ...

  3. java系列-安装MySql(三)

    第一大步:MySQL安装文件分为两种,一种是msi格式的,一种是zip格式的.如果是msi格式的可以直接点击安装,按照它给出的安装提示进行安装(相信大家的英文可以看懂英文提示),一般MySQL将会安装 ...

  4. 【PHP面向对象(OOP)编程入门教程】6.如何去使用对象中的成员

    上面看到PHP对象中的成员有两种一种是成员属性, 一种是成员方法.对象我们以经可以声明了,$p1=new Person(); 怎么去使用对象的成员呢?要想访问对象中的成员就要使用一个特殊的操作符”-& ...

  5. Java项目相关监控与调优

    Linux  JVM Tomcat =========Linux =============== 监控 nmon  命令:nmon -s 10 -c 60 -f -m /home -s 10 每10s ...

  6. RDS记录

    rdsafbnr32uzayn.mysql.rds.aliyuncs.com r7fysfqs90r74cm6

  7. leofs存储总结

    1.leofs角色 Account(账户).Bucket(对象桶).Object(对象), gateway.manager0.manager1.storage Account 一个account可以创 ...

  8. CSU 1337 搞笑版费马大定理(2013湖南省程序设计竞赛J题)

    题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1337 解题报告:虽然x和y的范围都是10^8,但是如果a 是大于1000的话,那么a^3 ...

  9. iOS开发——UI进阶篇(十六)Quartz2D实战小例子

    一.画线 只有在drawRect中才能获取到跟view相关联的上下文 - (void)drawRect:(CGRect)rect {} 一条线 // 1.获取跟当前View相关联的layer上下文(画 ...

  10. php中mysql与mysqli的区别

    两个函数都是用来处理DB 的.首先, mysqli 连接是永久连接,而mysql是非永久连接. mysql连接每当第二次使用的时候,都会重新打开一个新的进程,而mysqli则只使用同一个进程,这样可以 ...