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.


题解:dp+栈

用一个栈记录左括号的索引,每次匹配到")"的时候,弹出对应的左括号,并且用这个索引计算括号的长度。

用currentMax记录当前最长的匹配串,它可以由多个有效的匹配累加而成,比如"()(())",currentMax = 2 + 4 = 6;或者等于当前最大的匹配,比如"()((())",currentMax = 4;

leftLen表示匹配当前")"时,得到的最长匹配是多少,比如"()(())",leftLen = 4;

totalMax是最终最长的匹配长度,每次匹配到")"的时候更新。

遇到'(',压栈

遇到')'有三种情况:

  • 类似"())"或者")"的情况,即这个右括号是多余的,判断的条件是此时栈为空,说明重新开始了,把currentMax置零;
  • 类似"()()"或者"()(())"的情况,即这个右括号是匹配的,并且它匹配以后栈空了,说明前面搜索到的"()"可以和当前搜索完的串"()"或者"(())"合并起来,把currentMax += i - leftLen;
  • 类似"(()"或者"()(()",即这个右括号是匹配的,但是匹配后栈里面还有左括号,需要继续匹配,此时说明前面搜索到的"()"还不能和当前的"()"合并(二者中间有未匹配的左括号)。所以leftLen就是此时能得到的最大匹配长度了。

代码如下:

 public class Solution {
public int longestValidParentheses(String s) {
if(s==null || s.length() == 0)
return 0; Stack<Integer> stack = new Stack <Integer>();
int totalMax = 0;
int currentMax = 0; for(int i = 0;i < s.length();i++){
if(s.charAt(i) == '('){
stack.push(i);
}
else{
//situations like ")" or "())"
if(stack.isEmpty()){
currentMax = 0;
}
else{
int leftPos = stack.pop();
int leftLen = i - leftPos + 1; //situations like "()" or "()()",then we can accumulate with parathesis before
if(stack.isEmpty()){
currentMax += leftLen;
leftLen = currentMax;
}
//situations like "(()" or "(()()",there is still left parathesis, so we can't accumulate
else {
leftLen = i - stack.peek();
}
totalMax = Math.max(totalMax, leftLen); }
} } return totalMax;
}
}

【leetcode刷题笔记】Longest Valid Parentheses的更多相关文章

  1. 刷题32. Longest Valid Parentheses

    一.题目说明 题目是32. Longest Valid Parentheses,求最大匹配的括号长度.题目的难度是Hard 二.我的做题方法 简单理解了一下,用栈就可以实现.实际上是我考虑简单了,经过 ...

  2. 【leetcode刷题笔记】Valid Sudoku

    Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...

  3. 【leetcode刷题笔记】Valid Palindrome

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  4. 【leetcode刷题笔记】Valid Number

    Validate if a given string is numeric. Some examples:"0" => true" 0.1 " => ...

  5. 【LeetCode每天一题】Longest Valid Parentheses(最长有效括弧)

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

  6. leetcode解题报告 32. Longest Valid Parentheses 用stack的解法

    第一道被我AC的hard题!菜鸡难免激动一下,不要鄙视.. Given a string containing just the characters '(' and ')', find the le ...

  7. LeetCode (32) Longest Valid Parentheses

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

  8. LeetCode刷题笔记和想法(C++)

    主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...

  9. 18.9.10 LeetCode刷题笔记

    本人算法还是比较菜的,因此大部分在刷基础题,高手勿喷 选择Python进行刷题,因为坑少,所以不太想用CPP: 1.买股票的最佳时期2 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. ...

随机推荐

  1. Burp Suite基本用法

    从上一篇已经知道Burp Suite安装.启动方法,本章将会阐述Burp Suite抓包.重放.爆破.双参数爆破.爬虫等基本用法.同博客园看到一篇描述Burp Suite界面各个字段和按钮作用,感兴趣 ...

  2. FormatFloat

    http://www.delphibasics.co.uk/RTL.asp?Name=FormatFloat 1  function FormatFloat ( const Formatting : ...

  3. J2EE之字符编码输出

    1. public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletExcepti ...

  4. windows_64下python下载安装Numpy、Scipy、matplotlib模块

    本文应用的python3.6.3及其对应的Numpy.Scipy.matplotlib计算模块的cp36版本,其中Numpy是需要MKL版本的Numpy,这是后续安装Scipy的需要(本机系统win7 ...

  5. push推送服务设计

    PUSH系统架构设计简述 一.网络传输协议的选择 PUSH系统协议选取: UDP协议实时性更好,但是如何处理安全可靠的传输并且处理不同客户端之间的消息交互是个难题,实现起来过于复杂,那就非TCP协议莫 ...

  6. 如何搭建maven项目和搭建ssm框架

    1.基本概念 1.1.Spring Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One ...

  7. java实体类如果不重写toString方法,会如何?

    先认识一下Object Object 类的 toString 方法 返回一个字符串,该字符串由类名(对象是该类的一个实例).at 标记符“@”和此对象哈希码的无符号十六进制表示组成.换句话说,该方法返 ...

  8. vim visual模式 复制

    按ESC再按“V”,进入visual模式 用键盘向左向右箭头选中要复制的文字,按两下"Y"键 再到要粘贴的地方,按“P”键即可. 转自: http://jingyan.baidu. ...

  9. MySQL查询优化程序

    1.利用EXPLAIN 语句,查看是否用到索引: EXPLAIN 2.下面的WHERE 子句说明了怎样进行这项工作.第一行中,优化程序将简化表达式4/2 为值2,然后使用my_col 上的索引快速地找 ...

  10. 【BZOJ2806】[Ctsc2012]Cheat 广义后缀自动机+二分+单调队列优化DP

    [BZOJ2806][Ctsc2012]Cheat Description Input 第一行两个整数N,M表示待检查的作文数量,和小强的标准作文库的行数接下来M行的01串,表示标准作文库接下来N行的 ...