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.

分析:

给出一组括号,找出其中连续有效括号的最长长度。

for循环遍历每个括号,当前位置为i,index用于记录每段有效括号的起始位置。

(1)判断当前位置是否是左括号,如果遇到左括号,入栈。

(2)如果不是左括号,

1,判断栈是否为空,如果为空,将index指针后移。

2,如果栈不为空,弹出栈顶元素。

此时,如果栈为空,加上当前位置i的右括号可以构成一段有效的括号,最长为max(maxLen, i到index之间的距离),

如果栈不为空,说明栈顶后一位开始到当前位置可以构成一段有效的括号,那最长为max(maxLen, i到栈顶后一位的长度)。

public class Solution {
public int longestValidParentheses(String s) {
if (s == null){
return 0;
} Stack<Integer> p = new Stack<Integer>();
int maxLen = 0;
int index = 0; for (int i = 0; i < s.length(); i++){
if (s.charAt(i) == '('){
p.push(i);
}else{
if(p.isEmpty()){
index = i + 1;
}else{
p.pop();
if(p.isEmpty()){
maxLen = Math.max(i - index + 1, maxLen);
}else{
maxLen = Math.max(i - p.peek(), maxLen);
}
}
}
}
return maxLen;
}
}

leetcode 32. Longest Valid Parentheses的更多相关文章

  1. [LeetCode] 32. Longest Valid Parentheses 最长有效括号

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

  2. Java [leetcode 32]Longest Valid Parentheses

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

  3. [leetcode]32. Longest Valid Parentheses最长合法括号子串

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

  4. LeetCode 32 Longest Valid Parentheses(最长合法的括号组合)

    题目链接: https://leetcode.com/problems/longest-valid-parentheses/?tab=Description   Problem :已知字符串s,求出其 ...

  5. [LeetCode] 32. Longest Valid Parentheses (hard)

    原题链接 题意: 寻找配对的(),并且返回最长可成功配对长度. 思路 配对的()必须是连续的,比如()((),最长长度为2:()(),最长长度为4. 解法一 dp: 利用dp记录以s[i]为终点时,最 ...

  6. [Leetcode][Python]32: Longest Valid Parentheses

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 32: Longest Valid Parentheseshttps://oj ...

  7. leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、

    20. Valid Parentheses 错误解法: "[])"就会报错,没考虑到出现')'.']'.'}'时,stack为空的情况,这种情况也无法匹配 class Soluti ...

  8. [LeetCode] 032. Longest Valid Parentheses (Hard) (C++)

    指数:[LeetCode] Leetcode 指标解释 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 032. Lon ...

  9. 刷题32. Longest Valid Parentheses

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

随机推荐

  1. 【原】react+redux实战

    摘要:因为最近搞懂了redux的异步操作,所以觉得可以用react+redux来做一个小小的项目了,以此来加深一下印象.切记,是小小的项目,所以项目肯定是比较简单的啦,哈哈. 项目效果图如图所示:(因 ...

  2. angularjs中ng-change使用方法

    ng-change需与ng-model结合使用,官网说明如下:Note, this directive requires ngModel to be present. <label for=&q ...

  3. JS左右栏目添加器

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. 使用ASP.NET Web Api构建基于REST风格的服务实战系列教程【二】——使用Repository模式构建数据库访问层

    系列导航地址http://www.cnblogs.com/fzrain/p/3490137.html 前言 在数据访问层应用Repository模式来隔离对领域对象的细节操作是很有意义的.它位于映射层 ...

  5. 谁再说Matlab速度慢,我跟谁急

    谁再说Matlab速度慢,我跟谁急 一.未进行内存预分配 y = 0; tic; for i=2:100000; y(i+1) = y(i)+ randn; end; toc 时间已过 0.03122 ...

  6. Python开发【第五篇】:Python基础之杂货铺

    字符串格式化 Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存.[PEP-310 ...

  7. C# 对象 序列化 XML

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...

  8. Java RMI 介绍和例子以及Spring对RMI支持的实际应用实例

    RMI 相关知识 RMI全称是Remote Method Invocation-远程方法调用,Java RMI在JDK1.1中实现的,其威力就体现在它强大的开发分布式网络应用的能力上,是纯Java的网 ...

  9. 【转】使用Eclipse构建Maven项目 (step-by-step)

    安装eclipse 及配置maven时,参考的资料!!! from:http://blog.csdn.net/qjyong/article/details/9098213 Maven这个个项目管理和构 ...

  10. seo与sem的关系和区别

    seo与sem仅有一个字母之差,而且两者和网站优化都有很大的关系,很多初学者往往会把这2个名称弄混,即使一些做了多年的seo,有时候也无法区分这两者之间到底有何不同. 首先,我们从定义上来区分:SEO ...