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. 阿里云9折推荐码:0LGVW2

    阿里云9折推荐码:0LGVW2,第一次购买云服务器或云数据库可享受原价9折优惠.

  2. ScriptManager与UpdatePanel总结

    1.From http://www.cnblogs.com/Tim-Seven/archive/2011/02/11/1952409.html Ajax Extensions 2.ScriptMana ...

  3. spring学习

    http://blog.csdn.net/chjttony/article/details/6301523 http://blog.csdn.net/chjttony/article/details/ ...

  4. POJ 3034 Whac-a-Mole

    Whac-a-Mole Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 3070 Accepted: 922 Descriptio ...

  5. Magic Number(Levenshtein distance算法)

    Magic Number Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit ...

  6. linux 驱动程序中断号的申请

    1.linux下查看硬中断与软中断 硬中断:/proc/interrupts 软中断:/proc/softirqs 往往一些硬件中断号都是跟CPU所分配的硬件中断号相匹配的. 即同类型的cpu可能对同 ...

  7. H53D旋转-遁地龙卷风

    (-1)写在前面 首先图片是我从互联网上下载的,向这位前辈致敬.我用的是chrome49,没有加不同浏览器的前缀,jquery3.0,图片资源放在了我的百度云盘上http://pan.baidu.co ...

  8. linux 下开放端口问题

    Linux安装Tomcat后本地可以正常访问,可是这时Tomcat还不能被外界访问需要在Linux默认防护墙上打开8080端口 打开 /etc/sysconfig/iptables   [root@l ...

  9. iOS开发——项目篇—高仿百思不得姐 05——发布界面、发表文字界面、重识 bounds、frame、scrollView

    加号界面(发布模块) 一.点击加号modal出发布模块,创建控件,布局控件1)使用xib加载view,如果在viewDidLoad创建控件并设置frame 那么self.view 的宽高 拿到的是xi ...

  10. navigationcontroller剖析

    概述: 系统原生的navigationcontroller非常强大, 几乎所有的程序都基于系统的导航控制器做开发 它的navigationbar的有2种形态 navigationbar的frame其实 ...