参考:  1. https://leetcode.com/problems/longest-valid-parentheses/solution/

     2. https://blog.csdn.net/accepthjp/article/details/52439449

知道是用动态规划解决,但是写不出状态转移方程。。。

最后查看官方solution,理解了状态转移方程的由来!

 class Solution {
public:
int longestValidParentheses(string s) {
s = ")" + s;
// 在s前添加')'可以省去判断下标是否越界
vector<int> dp(s.size(), );
int res = , ans = ;
for (int i = ; i < s.size(); i++)
{
if (s[i] == ')')
if (s[i - ] == '(')
dp[i] = + dp[i - ];
else
{
if (s[i - dp[i - ] - ] == '(')
dp[i] = dp[i - ] + dp[i - dp[i - ] - ] + ;
}
ans = max(dp[i], ans);
}
return ans;
}
};

32. Longest Valid Parentheses最长有效括号的更多相关文章

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

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

  2. [leetcode]32. Longest Valid Parentheses最长合法括号子串

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

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

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

  4. 032 Longest Valid Parentheses 最长有效括号

    给一个只包含 '(' 和 ')' 的字符串,找出最长的有效(正确关闭)括号子串的长度.对于 "(()",最长有效括号子串为 "()" ,它的长度是 2.另一个例 ...

  5. leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、

    20. Valid Parentheses 错误解法: "[])"就会报错,没考虑到出现')'.']'.'}'时,stack为空的情况,这种情况也无法匹配 class Soluti ...

  6. 刷题32. Longest Valid Parentheses

    一.题目说明 题目是32. Longest Valid Parentheses,求最大匹配的括号长度.题目的难度是Hard 二.我的做题方法 简单理解了一下,用栈就可以实现.实际上是我考虑简单了,经过 ...

  7. [Leetcode][Python]32: Longest Valid Parentheses

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 32: Longest Valid Parentheseshttps://oj ...

  8. 32. Longest Valid Parentheses(最长括号匹配,hard)

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

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

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

随机推荐

  1. HTTP 返回状态码说明

    HTTP 返回状态码一.1xx - 信息提示 这些状态代码表示临时的响应.客户端在收到常规响应之前,应准备接收一个或多个 1xx 响应. • 100 - 继续. • 101 - 切换协议. 二.2xx ...

  2. error: could not create '/System/Library/Frameworks/Python.framework/Versions/2.7/share': Operation not permitted

    参考: Python pip安装模块报错 Mac升级到EI Captain之后pip install 无法使用问题 error: could not create '/System/Library/F ...

  3. [0403]学习一个——苟(简单Java开发)

    学习一个--苟 1. 开发目的 拜读了某神犇的blog,感到了自身深深的不足.蒟蒻如我,决定提高一蛤自身的姿势水平,学习一个,使用Java重写用GreatestLanguage写的某小说网站的抓取器. ...

  4. HDU 3635 Dragon Balls(带权并查集)

    http://acm.hdu.edu.cn/showproblem.php?pid=3635 题意: 有n颗龙珠和n座城市,一开始第i颗龙珠就位于第i座城市,现在有2种操作,第一种操作是将x龙珠所在城 ...

  5. Appium典型问题处理

    1. http://ask.testfan.cn/article/902 Appium 服务端安装-windows2. http://ask.testfan.cn/article/1078 最新版本a ...

  6. 【MySQL】【一】shell

    进入MySQL:mysql -u root -p 查看当前数据库下所有库:mysql> show databases; 切换到某库:mysql> use sys; 查看sys库下所有表:m ...

  7. c代码,输出i,j,k互不相同的三位数

    #include <stdio.h> int main() { int i,j,k; printf("\n"); for(i=1;i<5;i++){ for(j= ...

  8. Django中ORM简介与单表数据操作

    一. ORM简介  概念:.ORM框架是用于实现面向对象编程语言种不同类型系统的数据之间的转换 构建模型的步骤:重点 (1).配置目标数据库信息,在seting.py中设置数据库信息 DATABASE ...

  9. Sqlserver中分页,2012后支持offset + fetch,2012之前用rownum嵌套查询

    今天发现原先用的sql offset fetch好用,换了一个DB就歇菜 歇菜截图 比较了一下,是数据库版本的问题 一个是13,一个是10 版本低的不支持用offset + fetch 进行分页,ms ...

  10. Jtest的简单使用

    Jtest主要用于快速测试自己的代码是否正确 条件,导入相应的Jtest包 @Test    public void test() {        System.out.println(" ...