62.Longest Valid Parentheses(最长的有效括号)
Level:
Medium
题目描述:
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 "()()"
思路分析:
设置一个栈,遍历字符串,如果遇到‘(’,将它对应的下标存放进栈,遇到‘)’,其下标为i,弹出栈顶元素,计算 i-stack.pop()更新res,直到遍历结束,得到最大的res。
代码:
public class Solution{
public int longestValidParentheses(String s){
Stack<Integer>stack=new Stack<>();
int left=-1;
int res=0;
for(int i=0;i<s.length();i++){
if(s.charAt(i)=='(')
stack.push(i);
else{
if(stack.isEmpty())
left=i; //一开始出现‘)’
else{
stack.pop();
if(stack.isEmpty())
res=Math.max(res,i-left);
else
res=Math.max(res,i-stack.peek());
}
}
}
return res;
}
}
62.Longest Valid Parentheses(最长的有效括号)的更多相关文章
- [Leetcode] longest valid parentheses 最长的有效括号
Given a string containing just the characters'('and')', find the length of the longest valid (well-f ...
- [LeetCode] 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] 32. Longest Valid Parentheses 最长有效括号
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- 032 Longest Valid Parentheses 最长有效括号
给一个只包含 '(' 和 ')' 的字符串,找出最长的有效(正确关闭)括号子串的长度.对于 "(()",最长有效括号子串为 "()" ,它的长度是 2.另一个例 ...
- 32. Longest Valid Parentheses最长有效括号
参考: 1. https://leetcode.com/problems/longest-valid-parentheses/solution/ 2. https://blog.csdn.net/ac ...
- 刷题32. Longest Valid Parentheses
一.题目说明 题目是32. Longest Valid Parentheses,求最大匹配的括号长度.题目的难度是Hard 二.我的做题方法 简单理解了一下,用栈就可以实现.实际上是我考虑简单了,经过 ...
- Longest Valid Parentheses(最长有效括号)
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- [Swift]LeetCode32. 最长有效括号 | Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
随机推荐
- css浮动、定位到底什么鬼?
css操作元素位置有以下几种方式:float.position.top等. I float part 1.浮动首先会先将元素在正常文档流中删除,父容器无法获取元素高度,但是该元素依然影响布局. 2.任 ...
- 265-Keystone II JESD204B 66AK2L06 评估模块 (现行) XEVMK2LX
Keystone II JESD204B 66AK2L06 评估模块 (现行) XEVMK2LX 一. 板卡概述The XEVMK 2LX is a full-featured evaluation ...
- Mystery——团队作业——系统设计
这个作业属于哪个课程 https://edu.cnblogs.com/campus/xnsy/SoftwareEngineeringClass1 这个作业要求在哪里 https://edu.cnblo ...
- 前端学习(三十七)angular(笔记)
MVC 后台 M Module 数据层 V View 视图层 C Contro ...
- Java调用MySql数据库函数
Java调用MySql数据库函数 /** * 调用mysql的自定义函数 * */ private void test() { logger.info("show task start &q ...
- Python3.5-20190521-廖老师-自我笔记-单元测试
执行结果
- [BZOJ4826] [HNOI2017] 影魔 单调栈 主席树
题面 因为是一个排列,所以不会有重复的.如果有重复就没法做了.一开始没有仔细看题目想了半天. 发现,如果是第一种情况,那么边界\(l\)和\(r\)就应该分别是整个区间的最大值和次大值. 然后,对于那 ...
- CentOS7 安装xen(在虚拟机上成功,实体机测试死机!)
此文章只做操作记录,其中有些地方可能漏了!!我只贴出自己的操作过程!其它有差别的地方请自己网上查找参考! 只有在全虚拟化下才能安装Windows,这就需要有硬件支持,并在BIOS中开启Virtuali ...
- centos 6.5 解压 tar
只查看 tar 文件内容而不解压 tar -tvf filename.tar 解压到指定目录(没有指定则为当前目录) tar xvf filename.tar -C /usr/file 压缩为 tar ...
- 【NLP新闻-2013.06.03】New Book Where Humans Meet Machines
英语原文地址:http://nlp.hivefire.com/articles/share/39865/ 注:本人翻译NLP新闻只为学习专业英语和扩展视野,如果翻译的不好,请谅解! (我挺想看这本书的 ...