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. servlet/jsp GET/POST

    GET请求方式 当需要向服务器请求指定的资源时使用的方法 它不应该用于一些会造成副作用的操作中(在网络应用中用它来提交请求是一种常见的错误用法) 什么情况浏览器发送get请求 -在地址栏输入一个地址 ...

  2. js从一个函数中结束另一个函数的问题

    等待通过事件监听结束另一函数的方法出炉…… 1.事件监听? 2.从a里把b函数全局定义一下,不推荐. function a(){ alert("a"); b=function(){ ...

  3. Atitit.web三大编程模型 Web Page Web Forms 和 MVC

    Atitit.web三大编程模型 Web Page    Web Forms 和 MVC 1. 编程模型是 Web Forms 和 MVC (Model, View, Controller). 2.  ...

  4. 首先定义一个描述银行账户的Account类,包括成员变 量“账号”和“存款余额”,成员方法有“存款”、“取款”和“余额查询”。其次, 编写一个主类,在主类中测试Account类的功能。(已完善)

    package java1; public class Account { String zhanghao; double yue=0; double add; double get; Account ...

  5. 写hive db的两种方法

    方法1tmp_channel_hive_file="/tmp/tmp_channel_hive_file"tmp_channel_hive_file_new="/tmp/ ...

  6. jQuery单选框radio绑定click事件

    <div class="con_head"> <label><input type="radio" name="ask& ...

  7. Goodchild教授关于GIS的四大预测的不同看法

    Goodchild教授的关于GIS的4个未来发展的预测不断有人在微信朋友圈里转发,虽然现在做的工作GIS只是一个基本的工具之一了,但对这4个预测还是有不少不同看法和一点自己的意见. Goodchild ...

  8. Windows系统补丁KB2962872导致InstallShield无法启动(解决方案已更新)

    20140717最新更新: Flexera Software发布了临时补丁包,该补丁包暂时禁止了InstallShield Trialware功能(中国区用户很少有用此功能)两种安装方法: 方法1. ...

  9. Java 中的instanceof简单讲解

    Java 中的instanceof 运算符是用来在运行时指出对象是否是特定类的一个实例.instanceof通过返回一个布尔值来指出,这个对象是否是这个特定类或者是它的子类的一个实例. 用法:resu ...

  10. 解决adb shell input text 中文输入,unicode转utf-8

    https://github.com/senzhk/ADBKeyBoard 上面这个是外国人写的一个输入法,我们把它安装再设置下就ok了 直接下载bin下的ADBKeyBoard.apk文件,或者上面 ...