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 ...
随机推荐
- 从0构建webpack开发环境(三) 开发环境以及 webpack-dev-server 的使用
sourceMap 实际应用开发过程中大部分时间都是处于开发模式中,其中需要频繁的修改代码.调试和打包. 但是打包后的代码多个模块压缩到了一个bundle文件,如果出现警告或者异常很难定位到具体模块和 ...
- 使用纯php构建一个简单的PHP服务器
使用原生PHP构建一个简单的PHPWeb服务器 1.目录机构 webserver --src -- Response.php -- Server.php -- Request.php -- vendo ...
- idea模块搭建新手党常见错误
一.搭建java和web模块会出现的错误(此篇以分布式模块为例) 1.创建空工程 1.点击file ,在弹出的窗口左侧选项中在最后有一个Empty Project选项.此处就是创建空工程. 2.在此空 ...
- Sass-Opacity函数-rgba()函数
在前面介绍 RGB 函数一节中,还记得吗?有一个 rgba() 函数可以创建一个颜色,同时还可以对颜色修改其透明度.其可以接受两个参数,第一个参数为颜色,第二个参数是你需要设置的颜色透明值. > ...
- css 当文字过多时以....省略
<!-- 公告 --> <p class="rst-promotion">公告: {{shopInfo.rst.promotion_info}}</p ...
- TP、FP、FN、TN的含义
true positive(被正确分类的正例) false negative(本来是正例,错分为负例) true negative(被正确分类的负例) false positive(本来是负例,被错分 ...
- MySQL使用版本号实现乐观锁
原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11608581.html 乐观锁适用于读多写少的应用场景 乐观锁Version图示 Project D ...
- SpringMVC的 ModelAndView
使用ModelAndView类用来存储处理完后的结果数据,以及显示该数据的视图. public class ModelAndView { /** View instance or view name ...
- bzoj 2002[Hnoi2010]Bounce 弹飞绵羊(分治分块)
Description 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一条直线摆上n个装置,每个装置 ...
- webpack对icon-font图片的处理
一.对图片的处理 安装url-loader 然后再loaderli配置这样会把图片打包成base64格式 { test: /\.(gif|png|jpg)\??.*$/, loader: 'url-l ...