32. 最长有效括号

32. Longest Valid Parentheses

题目描述

给定一个只包含 '(' 和 ')' 的字符串,找出最长的包含有效括号的子串的长度。

每日一算法2019/6/3Day 31LeetCode32. Longest Valid Parentheses

示例 1:

输入: "(()"
输出: 2
解释: 最长有效括号子串为 "()"

示例 2:

输入: ")()())"
输出: 4
解释: 最长有效括号子串为 "()()"

Java 实现

import java.util.Stack;

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

相似题目

参考资料

LeetCode 32. 最长有效括号(Longest Valid Parentheses) 31的更多相关文章

  1. [Swift]LeetCode32. 最长有效括号 | Longest Valid Parentheses

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

  2. Java实现 LeetCode 32 最长有效括号

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

  3. LeetCode 32. 最长有效括号(Longest Valid Parentheses)

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

  4. leetcode:32 最长有效括号

     题目: 给一个包含了'(' 和 ')'的字符串,求出其中最长有效括号的长度. 做题情况:自己做出来,但做了较长的时间. 思路:可以算得穷举法的时间复杂度为O(n^3).虽然这题求的是最长的长度,但是 ...

  5. Leetcode——32.最长有效括号【##】

    @author: ZZQ @software: PyCharm @file: leetcode32_最长有效括号.py @time: 2018/11/22 19:19 要求:给定一个只包含 '(' 和 ...

  6. Leetcode 20题 有效的括号(Valid Parentheses) Java语言求解

    题目描述: 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭合. 左括号必须以正确的顺序闭合. 注意空 ...

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

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

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

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

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

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

随机推荐

  1. Codeforces Round #605 (Div. 3) B. Snow Walking Robot(构造)

    链接: https://codeforces.com/contest/1272/problem/B 题意: Recently you have bought a snow walking robot ...

  2. LeetCode 1143. Longest Common Subsequence

    原题链接在这里:https://leetcode.com/problems/longest-common-subsequence/ 题目: Given two strings text1 and te ...

  3. c++读写matlab中.mat数据

    前言:在进行图形图像处理时,经常会用到matlab进行算法的仿真验证,然后再移植到别的语言中.有时会涉及到数据的交互,比如直接读取matlab的.mat类型数据,或者是将c++中的数组存为.mat,为 ...

  4. HTTP协议(待写)

    先来了解了解 TCP/IP TCP/IP(Transmission Control Protocol / Internet Protocol)是计算机通讯必须遵守的规则,是不同的通信协议的大集合,其里 ...

  5. 1-ESP8266 SDK开发基础入门篇--开发环境搭建

    因为今天终于做好了自己的另一块工控板,所以我就开始写基础公开篇的内容,希望自己小小的努力能够帮到大家 自己做的另一块板子 https://www.cnblogs.com/yangfengwu/cate ...

  6. [RN] React Native 查看console打印出来的内容

    我们在调试React Native 程序的时候,经常会用到Js的打印语句Console.log等 但我们一脸蒙逼的时候,启动的时候完全看不到打印的内容在哪儿呢??? 原来还要在命令行下输入一个语句才能 ...

  7. 利用vue-meta管理头部标签

    在 Vue SPA 应用中,如果想要修改HTML的头部标签,或许,你会在代码里,直接这么做 // 改下title document.title = 'what?' // 引入一段script let ...

  8. tomcat9源码导入idea

    maven部署 下载源码 tomcat最新版的github地址 tomcat9官网下载 步骤 源码根目录新建 home 文件夹 把 conf 文件夹和 webapps 文件夹移动到 home 文件夹 ...

  9. Exception in thread "main" java.lang.IllegalStateException: Failed to read 问题解决

    开发中偶尔遇到这样的问题:Exception in thread "main" java.lang.IllegalStateException: Failed to read .. ...

  10. kafka 创建消费者报错

    kafka-console-consumer.sh --zookeeper master:2181,slave1:2181,slave2:2181 --topic test --from-beginn ...