题目

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]已经在上一步求解):

  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。
  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;
}
};

GitHub测试程序源码

LeetCode (32) Longest Valid Parentheses的更多相关文章

  1. LeetCode 笔记系列八 Longest Valid Parentheses [lich你又想多了]

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

  2. leetcode第31题--Longest Valid Parentheses

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

  3. LeetCode(32):最长有效括号

    Hard! 题目描述: 给定一个只包含 '(' 和 ')' 的字符串,找出最长的包含有效括号的子串的长度. 示例 1: 输入: "(()" 输出: 2 解释: 最长有效括号子串为 ...

  4. leetcode解题报告(7):Valid Parentheses

    描述 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the i ...

  5. Leetcode(32)-最长有效括号

    给定一个只包含 '(' 和 ')' 的字符串,找出最长的包含有效括号的子串的长度. 示例 1: 输入: "(()" 输出: 2 解释: 最长有效括号子串为 "()&quo ...

  6. LeetCode(5)Longest Palindromic Substring

    题目 Given a string S, find the longest palindromic substring in S. You may assume that the maximum le ...

  7. LeetCode(128) Longest Consecutive Sequence

    题目 Given an unsorted array of integers, find the length of the longest consecutive elements sequence ...

  8. LeetCode(3)Longest Substring Without Repeating Characters

    题目: Given a string, find the length of the longest substring without repeating characters. For examp ...

  9. LeetCode(14)Longest Common Prefix

    题目 Write a function to find the longest common prefix string amongst an array of strings. 分析 该题目是求一个 ...

随机推荐

  1. hdu3926 Hand in Hand 同构图

    #include<cstring> #include<cstdio> #include<algorithm> using namespace std; ]; str ...

  2. 跟我一起玩Win32开发(5):具有单选标记的菜单

    帅哥们,美女们,下午好,我又来误人子弟,请做好准备. 今天,我们的目的是,想要实现下图中的这种菜单效果. 就是一种类似单选按钮的菜单,多个菜单项中,同时只有一个会被选中. 首先,我们在资源编辑器中,设 ...

  3. Qt基本应用

    1 使用方式 在qt designer中直接设计图形界面,然后使用pyGUI转换成py文件. 可以发现,转换的文件为一个class.并不是一个完整的程序(运行时无法出现窗口).这个类名字是Ui_Mai ...

  4. Asp.net WebApi 异常处理解决方案

    一.使用异常筛选器捕获所有异常 我们知道,一般情况下,WebApi作为服务使用,每次客户端发送http请求到我们的WebApi服务里面,服务端得到结果输出response到客户端.这个过程中,一旦服务 ...

  5. Jury Meeting CodeForces - 854D

    Jury Meeting CodeForces - 854D 思路:暴力枚举会议开始的那一天(只需用所有向0点飞的航班的那一天+1去枚举即可),并计算所有人此情况下去0点和从0点出来的最小花费. 具体 ...

  6. LCA+树状数组 POJ 2763 Housewife Wind

    题目传送门 题意:两种操作,问u到v的距离,并且u走到了v:把第i条边距离改成w 分析:根据DFS访问顺序,将树处理成链状的,那么回边处理成负权值,那么LCA加上BIT能够知道u到v的距离,BIT存储 ...

  7. 18.3.2从Class上获取信息(属性)

    package d18_3_1; import java.lang.reflect.Field; import java.util.Arrays; /** * 获取Class对应类所包含的属性的四个方 ...

  8. 基于CentOS6.5下snort+barnyard2+base的入侵检测系统的搭建(图文详解)(博主推荐)

    为什么,要写这篇论文? 是因为,目前科研的我,正值研三,致力于网络安全.大数据.机器学习研究领域! 论文方向的需要,同时不局限于真实物理环境机器实验室的攻防环境.也不局限于真实物理机器环境实验室的大数 ...

  9. java只http改成https访问

    目录 生成keystore文件 修改tomcat中的server.xml文件 配置浏览器 生成keystore文件: 1.在tomcat的bin 目录下输入命令:keytool -genkeypair ...

  10. python的与或非运算

    真的很重要,栽了个跟头!!!(虽然以前好像知道...) print(True or False and False) print((True or False) and False) # True # ...