032 Longest Valid Parentheses 最长有效括号
给一个只包含 '(' 和 ')' 的字符串,找出最长的有效(正确关闭)括号子串的长度。
对于 "(()",最长有效括号子串为 "()" ,它的长度是 2。
另一个例子 ")()())",最长有效括号子串为 "()()",它的长度是 4。
详见:https://leetcode.com/problems/longest-valid-parentheses/description/
Java实现:
start变量来记录合法括号串的起始位置,遍历字符串,如果遇到左括号,则将当前下标压入栈,如果遇到右括号,如果当前栈为空,则将下一个坐标位置记录到start,如果栈不为空,则将栈顶元素取出,此时若栈为空,则更新结果和i - start + 1中的较大值,否则更新结果和i - 栈顶元素中的较大值.
class Solution {
public int longestValidParentheses(String s) {
if(s==null || s.length()==0){
return 0;
}
Stack<Integer> stack = new Stack<Integer>();
int start = 0;
int res = 0;
for(int i=0;i<s.length();i++){
if(s.charAt(i)=='('){
stack.push(i);
}else{
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://www.cnblogs.com/grandyang/p/4424731.html
032 Longest Valid Parentheses 最长有效括号的更多相关文章
- [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 ...
- 32. Longest Valid Parentheses最长有效括号
参考: 1. https://leetcode.com/problems/longest-valid-parentheses/solution/ 2. https://blog.csdn.net/ac ...
- [LeetCode] 032. Longest Valid Parentheses (Hard) (C++)
指数:[LeetCode] Leetcode 指标解释 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 032. Lon ...
- [Leetcode] longest valid parentheses 最长的有效括号
Given a string containing just the characters'('and')', find the length of the longest valid (well-f ...
- Java for LeetCode 032 Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- LeetCode 032 Longest Valid Parentheses
题目描述:Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the l ...
- Longest Valid Parentheses(最长有效括号)
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
随机推荐
- SQL中replace函数
string sql1 = "select price from dbo.eazy_farm where REPLACE(title,' ','')='" + cainame + ...
- CRT Library Features
CRT Library Features The new home for Visual Studio documentation is Visual Studio 2017 Documentatio ...
- python 基础 列表生成式
data = {'a':'abc';'b':'bac','c':'cba'} [v for k,v in data] 结果 ['abc','bca','cba'] 格式 [x for x in 内容 ...
- shell入门-连接符(并且、和、或者)
特殊符号:&& 说明:并且,左右两边是两条命令,左面的执行成功才会去执行右面的命令.右. 特殊符号:|| 说明:或者,左右两边是两条命令,左边的命令执行不成功,才会执行右面的命令 &a ...
- struts 文件上传示例
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io ...
- Java探索之旅(9)——数据和方法的可见性
注意,在UML图中,public-protected-private分别用+,-,#表示. 类中成员修饰符 在同一类访问 在同一包访问 在子类内访问 在不同包可访问 Public √ √ √ √ Pr ...
- .Net Core WebApi返回日期格式的问题
环境:.net core 2.1 webapi 问题简介: 返回DateTime,前端接收到的字符有时候为2018-01-01T12:01:01,有时候为2018-01-01T01:01:01.722 ...
- MAC电脑下Pycharm新建模板默认添加作者时间等信息
在pycharm使用过程中,对于每次新建文件的shebang行和关于代码编写者的一些个人信息快捷填写,使用模板的方式比较方便. 方法如下: 1.打开pycharm,选择Pycharm-preferen ...
- svg动画 animate
最近使用到svg的动画animate,简单总结下animate的主要属性: 1.定义:SVG 的animate 动画元素放在形状元素的内部,用来定义一个元素的某个属性如何踩着时点改变.在指定持续时间里 ...
- game with probability problem
两个人 A, B 取 n 枚石子,祂们轮流抛硬币 (A 先手),每次抛硬币,如果是正面,就取出一枚石子,否则什么都不做,然而 A, B 有一种超能力,在抛硬币前在意志中确定一面 (正面或反面),然后就 ...