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. 分析 该题目是求一个 ...
随机推荐
- 编译安装Apache:出现错误configure: error: mod_deflate
在进行编译安装Apache时,出现如下错误 checking whether to enable mod_deflate... configure: error: mod_deflate has be ...
- P1597 语句解析
题目背景 木有背景…… 题目描述 一串(<255)PASCAL语言,只有a,b,c 3个变量,而且只有赋值语句,赋值只能是一个一位的数字或一个变量,未赋值的变量值为0.输出a,b,c 最终的值. ...
- AJPFX总结java开发常用类(包装,数字处理集合等)(三)
4.Map是一种把键对象和值对象进行关联的容器,而一个值对象又可以是一个Map,依次类推,这样就可形成一个多级映射.对于键对象来说,像Set一样,一 个Map容器中的键对象不允许重复,这是为了保持查找 ...
- JavaWeb ,EL,
WEB 概述: java web 是用java 技术来解决相关web 互联网领域的技术总和 . web 可分为 web服务器和web客户端(浏览器) web 的资源分类: 静态资源: HTM ...
- 死磕 java并发包之AtomicInteger源码分析
问题 (1)什么是原子操作? (2)原子操作和数据库的ACID有啥关系? (3)AtomicInteger是怎么实现原子操作的? (4)AtomicInteger是有什么缺点? 简介 AtomicIn ...
- HTML的历史与历史遗留问题
1. <style type="text/css"> 从前,HTML的设计者认为以后应该还会有其他样式,不过如今我们已经醒悟,事实表明,完全可以只使用<style ...
- android studio MQTT测试成功
package myself.mqtt.wenzheng.studio.mqtt; import android.app.Notification; import android.app.Notifi ...
- toast插件的简单封装(样式适用pc后台管理系统的场景)
直接分三个步骤吧: 1.手写一个toast.vue组件 <template> <transition name="toast-fade"> <div ...
- iOS猜拳游戏源码
利用核心动画和Quartz2D做的一个小游戏.逻辑十分简单. 源码下载:http://code.662p.com/<ignore_js_op> 详细说明:http://ios.662p.c ...
- Java Web项目,Android和微信小程序的初始页面配置
Java Web项目 我们在Eclipse里开了Java Web项目之后,Run As Tomcat或者Apache服务器,本地运行,如果直接用http://localhost:8080访问项目,会发 ...