LeetCode 32. 最长有效括号(Longest Valid Parentheses) 31
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;
}
}
相似题目
参考资料
- https://leetcode-cn.com/problems/valid-parentheses/
- https://leetcode.com/problems/longest-valid-parentheses/
LeetCode 32. 最长有效括号(Longest Valid Parentheses) 31的更多相关文章
- [Swift]LeetCode32. 最长有效括号 | Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- Java实现 LeetCode 32 最长有效括号
32. 最长有效括号 给定一个只包含 '(' 和 ')' 的字符串,找出最长的包含有效括号的子串的长度. 示例 1: 输入: "(()" 输出: 2 解释: 最长有效括号子串为 & ...
- LeetCode 32. 最长有效括号(Longest Valid Parentheses)
题目描述 给定一个只包含 '(' 和 ')' 的字符串,找出最长的包含有效括号的子串的长度. 示例 1: 输入: "(()" 输出: 2 解释: 最长有效括号子串为 "( ...
- leetcode:32 最长有效括号
题目: 给一个包含了'(' 和 ')'的字符串,求出其中最长有效括号的长度. 做题情况:自己做出来,但做了较长的时间. 思路:可以算得穷举法的时间复杂度为O(n^3).虽然这题求的是最长的长度,但是 ...
- Leetcode——32.最长有效括号【##】
@author: ZZQ @software: PyCharm @file: leetcode32_最长有效括号.py @time: 2018/11/22 19:19 要求:给定一个只包含 '(' 和 ...
- Leetcode 20题 有效的括号(Valid Parentheses) Java语言求解
题目描述: 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭合. 左括号必须以正确的顺序闭合. 注意空 ...
- [LeetCode] 32. Longest Valid Parentheses 最长有效括号
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- [leetcode]32. Longest Valid Parentheses最长合法括号子串
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- [LeetCode] Longest Valid Parentheses 最长有效括号
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
随机推荐
- The Business Of Open Source
http://oss-watch.ac.uk/resources/businessofopensource by Matthew Langham, Indiginox on 3 February 20 ...
- fitnesse的安装
最近项目组有个单独的功能模块需要写自动化,由于是测试接口,我本来是想用之前那个项目组使用的robot framework+python,但是呢,项目组领导觉得,目前项目开发语言是java,相应的自动化 ...
- LeetCode 1049. Last Stone Weight II
原题链接在这里:https://leetcode.com/problems/last-stone-weight-ii/ 题目: We have a collection of rocks, each ...
- Java web开发——文件夹的上传和下载
我们平时经常做的是上传文件,上传文件夹与上传文件类似,但也有一些不同之处,这次做了上传文件夹就记录下以备后用. 这次项目的需求: 支持大文件的上传和续传,要求续传支持所有浏览器,包括ie6,ie7,i ...
- 2019.12.09 java for循环
for(初始化表达式; 循环条件; 操作表达式){ 执行语句 ……… } 先走初始化表达式,再走循环条件,如条件满足,走执行语句,然后走操作表达式,再走循环条件,如条件满足,走执行语句 ...
- hibernate之一对多关系
1. 什么是关联(association) 1.1 关联指的是类之间的引用关系.如果类A与类B关联,那么被引用的类B将被定义为类A的属性.例如: public class A{ private B b ...
- Python 05 Geany的基本使用1
问题01:代码中包含中文编译时提示错误 原文:https://blog.csdn.net/weixin_43345286/article/details/82951698 解决:文档 - 设置文件编码 ...
- [RN] React Native 下实现底部标签(不支持滑动切换)
底部标签是现在App的基本菜单实现 下面分别用 createBottomTabNavigator 和 createMaterialBottomTabNavigator 两种方法分别实现底部菜单 但此两 ...
- Cogs 732. [网络流24题] 试题库(二分图)
[网络流24题] 试题库 ★★ 输入文件:testlib.in 输出文件:testlib.out 评测插件 时间限制:1 s 内存限制:128 MB «问题描述: 假设一个试题库中有n道试题.每道试题 ...
- Python入门(一)-打开世界之Hello World
关注我,每天都有优质技术文章推送,工作,学习累了的时候放松一下自己. 本篇文章同步微信公众号 欢迎大家关注我的微信公众号:「醉翁猫咪」 今天我们来用Python向世界说声Hello World,人生 ...