参考:  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. Images之Dockerfiles

    Best practices for writing Dockerfiles This document covers recommended best practices and methods f ...

  2. Kubernetes相关概念

    This page explains how Kubernetes objects are represented in the Kubernetes API, and how you can exp ...

  3. dplyr-高效的数据变换与整理工具--转载

    1.背景简介 在数据分析工作中,经常需要对原始的数据集进行清洗.整理以及变换.常用的数据整理与变换工作主要包括:特定分析变量的选取.满足条件的数据记录的筛选.按某一个或几个变量排序.对原始变量进行加工 ...

  4. 将.db文件导入SQLServer2008数据库

    最近要做一个项目,需要连接数据库,给我的数据文件是sqlite,我需要将数据导入到SQLServer数据库 需要借助一个软件:DBDBMigration 页面最上方的选择框内,先选择数据文件类型,这里 ...

  5. leetcode 29-> Divide Two Integers without using multiplication, division and mod operator

    class Solution(object): def divide(self, dividend, divisor): """ :type dividend: int ...

  6. IIS7 配置Http重定向到Https

    1.注意首先要安装url重定向模块 微软官方地址:https://www.microsoft.com/zh-CN/download/details.aspx?id=7435 百度网盘地址:链接: ht ...

  7. scikit_learn逻辑回归类库

    来自:刘建平 1.概述 在scikit-learn中,与逻辑回归有关的主要有3个类.LogisticRegression, LogisticRegressionCV 和 logistic_regres ...

  8. ASP.net MVC模式介绍(一)

    一.ASP.NET 支持三种不同的开发模式:Web Pages(Web 页面).MVC(Model View Controller 模型-视图-控制器)表现层.Web Forms(Web 窗体) mv ...

  9. TypeError: add() argument after * must be an iterable, not Settings的错误原因

    在抄代码的时候发现有个错误: TypeError: add() argument after * must be an iterable, not Settings 看不懂,百度才知道原因,原来是第2 ...

  10. 对Maven项目进行强制更新

    选中项目 然后 选中Force Update of Snapshots/Releases,点击OK即可. 此时pom.xml就不会报错了.