LeetCode (32) Longest Valid Parentheses
题目
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]。
AC代码
class Solution {
public:
int longestValidParentheses(string s) {
if (s.empty())
return 0;
//求括号字符串长度
int len = s.length();
//定义一个长度vector,i处值计量从i开始的到len-1长度字符串的最长有效匹配括号长度
vector<int> dp(len, 0);
int maxlen = 0;
for (int i = len - 2; i >= 0; --i)
{
if (s[i] == '(')
{
int j = i + 1 + dp[i + 1];
if (j < len && s[j] == ')')
{
dp[i] = dp[i + 1] + 2;
if (j + 1 < len)
dp[i] += dp[j + 1];
}
}
//实时求最长有效匹配长度
if (dp[i] > maxlen)
maxlen = dp[i];
}//for
return maxlen;
}
};
LeetCode (32) Longest Valid Parentheses的更多相关文章
- LeetCode 笔记系列八 Longest Valid Parentheses [lich你又想多了]
题目:Given a string containing just the characters '(' and ')', find the length of the longest valid ( ...
- leetcode第31题--Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- LeetCode(32):最长有效括号
Hard! 题目描述: 给定一个只包含 '(' 和 ')' 的字符串,找出最长的包含有效括号的子串的长度. 示例 1: 输入: "(()" 输出: 2 解释: 最长有效括号子串为 ...
- leetcode解题报告(7):Valid Parentheses
描述 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the i ...
- Leetcode(32)-最长有效括号
给定一个只包含 '(' 和 ')' 的字符串,找出最长的包含有效括号的子串的长度. 示例 1: 输入: "(()" 输出: 2 解释: 最长有效括号子串为 "()&quo ...
- LeetCode(5)Longest Palindromic Substring
题目 Given a string S, find the longest palindromic substring in S. You may assume that the maximum le ...
- LeetCode(128) Longest Consecutive Sequence
题目 Given an unsorted array of integers, find the length of the longest consecutive elements sequence ...
- LeetCode(3)Longest Substring Without Repeating Characters
题目: Given a string, find the length of the longest substring without repeating characters. For examp ...
- LeetCode(14)Longest Common Prefix
题目 Write a function to find the longest common prefix string amongst an array of strings. 分析 该题目是求一个 ...
随机推荐
- Linux 命令行光标操作
转自: https://blog.csdn.net/leo_618/article/details/53003111 看一个真正的专家操作命令行绝对是一种很好的体验-光标在单词之间来回穿梭,命令行不同 ...
- hdu 1558 Segment set 计算几何+并查集★
#include <cstdio> #include <iostream> #include <string.h> using namespace std; ; # ...
- JDBC全部分析
2.1.搭建实验环境 1.在mysql中创建一个库,并创建user表和插入表的数据. SQL脚本如下: 1 create database jdbcStudy character set utf8 c ...
- Linux下文件权限的设置
文件/目录权限设置命令:chmod 这是Linux系统管理员最常用到的命令之一,它用于改变文件或目录的访问权限.该命令有两种用法: 用包含字母和操作符表达式的文字设定法 ) 其语法格式为:chmod ...
- Tomcat+Jenkins+SonarQube+SVN+Maven 集成自动化环境搭建(Windows10环境下)
说在前面的话: 从接到任务到完成共用了7天的时间.正常人用不到这个时间. 此时的功能表现是: 登录本地JenKins对项目进行构建,能够自动从SVN读取最新代码并按照Maven项目构建,构建完成能够自 ...
- AJPFX总结面向对象中成员变量和成员方法的定义
//面向对象中成员变量和成员方法的定义格式:========================================= 成员变量定义在类中方法外,可以被该类中所有方法使用. ...
- 微信小程序 插件介绍
小程序的插件是对一组js接口.自定义组件或页面的封装.插件不能独立运行,必须嵌入在其他小程序中才能被用户使用:而第三方小程序在使用插件时,也无法看到插件的代码.因此,插件适合用来封装自己的功能或服务, ...
- EditText输入手机号自动带空格
xml: <EditText android:id="@+id/edit_main" android:layout_width="match_parent" ...
- 【python】入门级识别验证码
前情:这篇文章所提及的内容是博主上个暑假时候做的,一直没有沉下心来把自己的心得写在纸面上,所幸这个假期闲暇时候比较多,想着能写多少是多少,于是就有了此篇. 验证码?我也能破解? 关于验证码的介绍就不多 ...
- TCAM 与CAM
CAM是Content Addressable Memory的缩写,即"内容寻址存储器"的意思,它是在传统的存储技术的基础上实现的联想记忆存储器,关于CAM的基本操作有三种: 1) ...