Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring.

Example 1:

Input: "(()"
Output: 2
Explanation: The longest valid parentheses substring is "()"

Example 2:

Input: ")()())"
Output: 4
Explanation: The longest valid parentheses substring is "()()"
class Solution {
public int longestValidParentheses(String s) {
if (s == null || s.length() == 0) {
return 0;
} LinkedList<Integer> stack = new LinkedList<>();
int j = -1, res = 0;
for (int i = 0; i < s.length(); i++) {
char cur = s.charAt(i);
if (cur == '(') {
stack.offerFirst(i);
} else if (cur == ')' && stack.isEmpty()) {
j = i;
} else {
stack.pollFirst();
if (stack.isEmpty()) {
res = Math.max(res, i - j);
} else {
res = Math.max(res, i - stack.peekFirst());
}
}
}
return res;
}
}

[LC] 32. Longest Valid Parentheses的更多相关文章

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

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

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

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

  3. 刷题32. Longest Valid Parentheses

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

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

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

  5. leetcode 32. Longest Valid Parentheses

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

  6. 32. Longest Valid Parentheses

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

  7. Java [leetcode 32]Longest Valid Parentheses

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

  8. leetcode problem 32 -- Longest Valid Parentheses

    Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...

  9. 【Python】32. Longest Valid Parentheses

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

随机推荐

  1. 增删改查(简单版&连接数据库)

    这个博客也是补充之前的学习内容: 项目总述:这个增删改查我以,选课名称,选课教室,选课教师基本信息,作为主要的信息来源.主要对这些信息最基本的增删改查 详细的分析与说明: 1.首先在src文件里定义四 ...

  2. python中的API学习

    URL: url是统一资源定位符,对可以从互联网上得到的资源的位置和访问方法的一种简洁的表示,是互联网上标准资源的地址.互联网上的每个文件都有一个唯一的URL,它包含的信息指出文件的位置以及浏览器应该 ...

  3. 面试易错题 Java

    1. int[] arr = new int[10]; System.out.println(arr);//地址值? char[] arr1 = new char[10]; System.out.pr ...

  4. 86.QuerySet API常用的方法详解:get方法

    get方法的查询条件只能有一条数据满足,如果匹配到多条数据都满足,就会报错:如果没有匹配到满足条件的数据,也会报错. 示例代码如下: from django.http import HttpRespo ...

  5. 17.3.10--C语言运行的步骤

    编译-->生成-->调试-->链接-->运行 编译就是:将你编写的C语言程序翻译成机器能识别运行的指令集 生成就是:根据编译完成的指令集制造出机器可以具体执行的指令序列 调试就 ...

  6. 蓝桥杯2015-省赛-C/C++-A组2题 星系炸弹

    在X星系的广袤空间中漂浮着许多X星人造“炸弹”,用来作为宇宙中的路标.每个炸弹都可以设定多少天之后爆炸.比如:阿尔法炸弹2015年1月1日放置,定时为15天,则它在2015年1月16日爆炸.有一个贝塔 ...

  7. spring 事物面试题

    1.spring 事物管理器中事物传播机制 2.spring中事物的隔离级别 读未提交-事物未提交,另一个事物可以读取到,脏读 读已提交-事物已提交,先前读取的数据与后来读取的数据不同,不可重复读 可 ...

  8. Spring AOP中使用args表达式访问目标方法的参数

    Spring AOP 的使用过程理解 首先,aop的使用场景介绍: 1.处理一些通用的非功能性的需求,不影响业务流程,比如说打印日志.性能统计.推送消息等: 2.aop无法拦截static.final ...

  9. 剑指offer【11】- 矩形覆盖

    题目:我们可以用2*1的小矩形横着或者竖着去覆盖更大的矩形.请问用n个2*1的小矩形无重叠地覆盖一个2*n的大矩形,总共有多少种方法? 在分析前不知道是什么序列,所以先看了n=1,n=2,n=3,n= ...

  10. 关于UDP通信的参考目录

    1.IP头,TCP头,UDP头,MAC帧头定义 2.深入理解TCP/UDP通信原理 其内部有提到关于wireshark抓包分析工具的使用 3.udp通讯中的connect()和bind()函数 其中有 ...