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.

Hide Tags

Dynamic Programming String

 

  其实就是判断最常合法长度
如果当前字符为'(' 忽略。
如果为 ')':
1. 前一字符为 '(',那么长度是前二字符的最长合法长度+2.
2. 前一字符为')',那么获得前一字符的最长合法长度tmpLen
  a. 如果 前tmpLen+1  项是'(',那么便是合法的,取值为前tmpLen+1+1 项的长度  + tmpLen+2.
  b. 为')',配对失败,忽略。
 
最后返回最长的。
 
#include <string>
#include <iostream>
#include <vector>
using namespace std; class Solution {
public:
int longestValidParentheses(string s) {
int len = s.length();
if(len<) return ;
vector<int > table(len+,);
int cnt = ;
if(s[]=='(') cnt++;
else cnt --;
int retMax = ;
for(int i=;i<len;i++){
if(s[i]=='('){
if(cnt<) cnt=;
else cnt++;
continue;
}
cnt--;
if(cnt>=){
if(s[i-]=='(') table[i+] = table[i-]+;
else{
if(s[i--table[i]]=='(')
table[i+] = table[i--table[i]]++table[i];
}
if(retMax<table[i+]) retMax = table[i+];
}
}
return retMax;
}
}; int main()
{
Solution sol;
cout<<sol.longestValidParentheses("()(())")<<endl;
return ;
}

[LeetCode] Longest Valid Parentheses 动态规划的更多相关文章

  1. [LeetCode] Longest Valid Parentheses 最长有效括号

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  2. [Leetcode] longest valid parentheses 最长的有效括号

    Given a string containing just the characters'('and')', find the length of the longest valid (well-f ...

  3. [LeetCode] Longest Valid Parentheses 解题思路

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  4. [LeetCode] Longest Valid Parentheses

    第一种方法,用栈实现,最容易想到,也比较容易实现,每次碰到‘)’时update max_len,由于要保存之前的‘(’的index,所以space complexity 是O(n) // 使用栈,时间 ...

  5. LeetCode: Longest Valid Parentheses 解题报告

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

  6. [LeetCode] Longest Valid Parentheses -- 挂动态规划羊头卖stack的狗肉

    (Version 1.3) 这题在LeetCode上的标签比较有欺骗性,虽然标签写着有DP,但是实际上根本不需要使用动态规划,相反的,使用动态规划反而会在LeetCode OJ上面超时.这题正确的做法 ...

  7. leetcode解题报告 32. Longest Valid Parentheses 动态规划DP解

    dp[i]表示以s[i]结尾的完全匹配的最大字符串的长度. dp[] = ; ; 开始递推 s[i] = ')' 的情况 先想到了两种情况: 1.s[i-1] = '(' 相邻匹配 这种情况下,dp ...

  8. leetcode: Longest Valid Parentheses分析和实现

    题目大意:给出一个只包含字符'('和')'的字符串S,求最长有效括号序列的长度. 很有趣的题目,有助于我们对这种人类自身制定的规则的深入理解,可能我们大多数人都从没有真正理解过怎样一个括号序列是有效的 ...

  9. leetcode Longest Valid Parentheses python

    class Solution(object): def longestValidParentheses(self, s): """ :type s: str :rtype ...

随机推荐

  1. [Python爬虫] Selenium实现自动登录163邮箱和Locating Elements介绍

    前三篇文章介绍了安装过程和通过Selenium实现访问Firefox浏览器并自动搜索"Eastmount"关键字及截图的功能.而这篇文章主要简单介绍如何实现自动登录163邮箱,同时 ...

  2. paip.提升效率--批量变量赋值 “多元”赋值

    paip.提升效率--批量变量赋值 "多元"赋值 ##石麻是批量变量赋值. 为一组变量赋值. 例子 1 <?php $my_array = array("Dog&q ...

  3. paip.文件目录操作uAPI php python java对照

    paip.文件目录操作uAPI php python java对照 chdir -- 改变目录 chroot -- 改变根目录 dir -- directory 类 closedir -- 关闭目录句 ...

  4. GUID全局唯一标识符相关知识了解

     全局唯一标识符(GUID,Globally Unique Identifier)是一种由算法生成的二进制长度为128位的数字标识符.GUID主要用于在拥有多个节点.多台计算机的网络或系统中.在理想情 ...

  5. MFC可编辑ListBox控件CEditableListBox

    左击选中单元格,右击进入编辑状态. MFC自定义控件的添加方法C#自定义控件编译后就自动出现在工具箱里.MFC的自定义控件需要先拖个基类到对话框上,然后添加一个控件变量.再修改源代码中的控件名为扩展控 ...

  6. MySQL vs. MongoDB: Choosing a Data Management Solution

    原文地址:http://www.javacodegeeks.com/2015/07/mysql-vs-mongodb.html 1. Introduction It would be fair to ...

  7. 如何在maven项目的pom.xml文件中添加jar包

    在使用maven进行项目开发时,我们需要在pom.xml文件中添加自己所需要的jar包.这就要求我们获取jar包的groupId和artifactId. 我们可以在一些maven仓库上搜索我们所需要的 ...

  8. [C] c99int(让VC等编译器自动兼容C99的整数类型)V1.02。源码托管到github、添加CMake编译配置文件、使用doxygen规范注释

    新版本—— http://www.cnblogs.com/zyl910/p/zlstdint_v100.html[C] zlstdint(让VC.TC等编译器自动兼容C99的整数类型)V1.0.支持T ...

  9. 单点登录 SSO(Single Sign-On)的实现原理

    为什么要 SSO? 企业的信息化过程是一个循序渐进的过程,这就造成在企业的不同时期,根据业务和发展需要,构建了多个应用程序,而这些应用程序在功能.设计和技术可能都有所不同,就形成了各自独立的用户库和用 ...

  10. 高级屏幕空间反射: Screen Space Reflection (SSSR)

    SSSR进一步调优,对标寒霜级技术水平,实现方式为Direct3D 11+自主实现实时渲染引擎,方法为对比测试.实现已经有段时间了,还是简要更新下吧.以下画面中的SSSR效果全部采用1:4 resol ...