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. 分析 该题目是求一个 ...
随机推荐
- mysql 用 group by 和 order by同时使用
首先,这是不可能实现的 mysql的查询的顺序 select -> from-> where->group by->having->order by. 但mysql的解析 ...
- visual studio各版本下载
软件包括以下几种: cn_visual_studio_2010_ultimate_x86_dvd_532347.part1.rar cn_visual_studio_2010_ultimate_x86 ...
- inline-block元素水平居中问题
今天做项目的时候碰到了不固定元素个数,需要水平居中的问题,原来的确定宽度下margin:0 auto等方法木有用了.想起来之前看过display:inline-block的文章, 果断用这个. 之前很 ...
- python计算代码运行时间的装饰器
import time def cal_time(func): def wrapper(*args, **kwargs): t1 = time.time() result = func(*args, ...
- 第02课 操作系统及Linux 系统介绍
1.操作系统介绍 操作系统(Operating System,简称OS),是计算机系统中必不可少的基础系统软件,它是应用程序运行以及用户操作必备的基础环境支撑,是计算机系统的核心. 操作系统的作用是管 ...
- bash 变量传递方法
###1.sh ##(该sh 目的是 将变量env传入env.sh, 同时让env.sh在当前事物生效,最后执行env.sh 定义的变量envs) export ENV=prepareecho ...
- 使用callabestatement接口调用存储过程
- [转]VC++的类头文件
本文转自:http://blog.csdn.net/forevertali/article/details/4370602 animal.h //在头文件中包含类的定义及类成员函数的声明 clas ...
- 使用grunt构建前端项目
1. grunt构建工具是基于nodejs上的,所以在使用之前一定要先安装好nodejs 2. 安装好nodejs后,node -v查看node版本 npm-v 查看npm版本信息 3. 在需要用到的 ...
- AJPFX总结java 中类的创建和使用
//面向对象中类的创建和 调用 ============================================================= 类的使用: 类的使用分为两个动作:创建对象与 ...