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.

这道题好理解,但是要自己想的话还是很难想出来的,动态规划说白了就是记录当前的结果,留给后面的用。

求最长合法匹配的长度,这道题可以用一维动态规划逆向求解。假设输入括号表达式为String s,维护一个长度为s.length的一维数组dp[],数组元素初始化为0。 dp[i]表示从s[i]到s[s.length - 1] 包含s[i] 的最长的有效匹配括号子串长度。则存在如下关系:

  • dp[s.length - 1] = 0;
  • i从n - 2 -> 0逆向求dp[],并记录其最大值。若s[i] == '(',则在s中从i开始到s.length - 1计算dp[i]的值。这个计算分为两步,通过dp[i + 1]进行的(注意dp[i + 1]已经在上一步求解):
    • 在s中寻找从i + 1开始的有效括号匹配子串长度,即dp[i + 1],跳过这段有效的括号子串,查看下一个字符,其下标为j = i + 1 + dp[i + 1]。若j没有越界,并且s[j] == ‘)’,则s[i ... j]为有效括号匹配,dp[i] =dp[i + 1] + 2。
    • 在求得了s[i ... j]的有效匹配长度之后,若j + 1没有越界,则dp[i]的值还要加上从j + 1开始的最长有效匹配,即dp[j + 1]。
      class Solution {
      public:
      int longestValidParentheses(string s) {
      int len=s.size();
      vector<int >res(len,);
      int finalRes=;
      for(int i = len-;i>=;i--)
      {
      if(s[i]=='(')
      {
      int end=i + + res[i + ];
      if(end<len&&s[end]==')')
      {
      res[i]=res[i+]+;
      if(end+<len)
      res[i]+=res[end+];
      finalRes=max(finalRes,res[i]);
      } }
      }
      return finalRes;
      }
      };

      最简单的方法:当碰到")"时,向左遍历找到未标记过的"(",则将这两个位置标为1,然后统计1连续出现的长度即可,时间复杂度为O(n^2)。

      http://blog.csdn.net/cfc1243570631/article/details/9304525

Longest Valid Parentheses——仍然需要认真看看(动态规划)的更多相关文章

  1. LeetCode之“动态规划”:Valid Parentheses && Longest Valid Parentheses

    1. Valid Parentheses 题目链接 题目要求: Given a string containing just the characters '(', ')', '{', '}', '[ ...

  2. [LeetCode] Longest Valid Parentheses 动态规划

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

  3. 动态规划——Longest Valid Parentheses

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

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

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

  5. 【leetcode】 Longest Valid Parentheses (hard)★

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

  6. Longest Valid Parentheses 每每一看到自己的这段没通过的辛酸代码

    Longest Valid Parentheses My Submissions Question Solution  Total Accepted: 47520 Total Submissions: ...

  7. 【Longest Valid Parentheses】cpp

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

  8. Longest Valid Parentheses(最长有效括号)

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

  9. LeetCode32 Longest Valid Parentheses

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

随机推荐

  1. 剑桥offer系列(1~10)

    1.题目描述 在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数. 思路:从左下开始, ...

  2. git安装配置和使用

    ## 安装git服务器 ## 安装git sudo apt-get install git ## 建立git用户 sudo adduser git ## 修改git用户 * 设置不能登录 vim /e ...

  3. JQuery学习二(获取元素控件并控制)

    $(’#id‘).+function; 例如: 1 <head> 2 <title>JQuery</title> 3 <script src="js ...

  4. Scrapy中的Callback如何传递多个参数

    在scrapy提交一个链接请求是用 Request(url,callback=func) 这种形式的,而parse只有一个response参数,如果自定义一个有多参数的parse可以考虑用下面的方法实 ...

  5. SSH 指定密钥,连接远程服务器。

    ssh -i /root/.ssh/private.pem user@192.168.1.100 -p 7744 如上, /root/.ssh/private.pem :密钥文件路径user@192. ...

  6. spring事务的一些注意点

    参考文章 http://blog.csdn.net/qq_34021712/article/details/75949779   ©王赛超 1.在需要事务管理的地方加@Transactional 注解 ...

  7. UVA 11440 Help Tomisu

    https://vjudge.net/problem/UVA-11440 题意: 求2——n! 之间有多少个整数x,满足x的所有素因子都大于m 保证m<=n x的所有素因子都大于m 等价于 x和 ...

  8. bzoj 1705: [Usaco2007 Nov]Telephone Wire 架设电话线——dp

    Description 最近,Farmer John的奶牛们越来越不满于牛棚里一塌糊涂的电话服务 于是,她们要求FJ把那些老旧的电话线换成性能更好的新电话线. 新的电话线架设在已有的N(2 <= ...

  9. Spring基础使用(一)--------IOC、Bean的XML方式装配

    基础 1.xml文件基础格式: <?xml version="1.0" encoding="UTF-8" ?> <beans xmlns=&q ...

  10. 微信小程序提示框

    一.wx.showToast 如上图所示,showToast会显示一个弹窗,在指定的时间之后消失.中间的图标默认只有加载中和成功两种,也可以用image参数自定义图标 wx.showToast({ t ...