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. ZJNU 2354 - 进贡

    分开考虑k=1 k=2和k>=3的情况 2和3这两个质数比较特殊,遇到的话直接输出1就行 对于“神灵的不满意度为m的约数中,比m小且最大的那个”这句描述,指m除了自身和1这两个因子里找最大的那个 ...

  2. linux 解压命令总结

    常用Linux 命令: [转自]https://www.jianshu.com/p/ca41f32420d6 解压缩 .tar 解包:tar xvf FileName.tar 打包:tar cvf F ...

  3. Istio流量治理原理之负载均衡

    流量治理是一个非常宽泛的话题,例如: ● 动态修改服务间访问的负载均衡策略,比如根据某个请求特征做会话保持: ● 同一个服务有两个版本在线,将一部分流量切到某个版本上: ● 对服务进行保护,例如限制并 ...

  4. PHP实现快速排序算法相关案例

    <?php /** * 快速排序 --主要运用递归, 先把一个数找准位置,然后再递归把左右两边的数都找准位置 */ function QSort($a= []){ $nCount = count ...

  5. centos 下使用 pytesseract 识别文字

    偶发一个想法搭一个验证码识别工具,网上查了一下有Tesseract 这个工具可以识别,所以有了后面一小时的搭建过程 ps:Ubuntu 下似乎可以直接用包管理工具来安装,我使用的源码编译安装 前提 由 ...

  6. Codeforces 1294E - Obtain a Permutation

    题目大意: 给定一个n*m的矩阵 可以更改任意一个位置的值 也可以选择一整列全部往上移动一位,最上方的数移动到最下方 问最少操作多少次可以把这个矩阵移动成 1 2 3 ... m m+1 m+2 m+ ...

  7. python-day1 爬虫基础之HTTP基本原理

    经过前几天的开发环境配置,今天终于正式开启学Python之路了,今天主要看了HTTP的基本原理,下边做一个总结: 1.首先要了解的就是URI和URL,URI的全拼是Uniform Resource I ...

  8. StartDT AI Lab | 需求预测引擎如何助力线下零售业降本增效?

    在当下经济明显进入存量博弈的阶段,大到各经济体,小到企业,粗放的增长模式已不适宜持续,以往高增长的时代已经成为过去,亟需通过变革发掘新的增长点.对于竞争激烈的线下零售行业而言,则更需如此. 零售行业一 ...

  9. jupyter notebook的扩展插件

    具体安装使用,请参考 https://github.com/ipython-contrib/IPython-notebook-extensions

  10. Clairaut 定理 证明

    (Clairaut 定理)设 $E$ 是 $\mathbf{R}^n$ 的开子集合,并设 $f:\mathbf{E}\to \mathbf{R}^{m}$ 是 $E$ 上的二次连续可微函数.那么对于一 ...