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 ...
随机推荐
- #include <deque>
deque \(deque\)头文件主要包括一个双端队列容器.是一个支持在两端插入两端删除的线性储存空间,与vector和queue相似.与\(vector\)比起来,\(deque\)可以在\(O( ...
- mount error(12): Cannot allocate memory解决办法
http://hi.baidu.com/zhangbin101004/item/e459f4d1f818dfbd33db903b 今天囧了啊,在ubuntu挂载的文件夹里面解压数据库,结果linux嫌 ...
- Spring 3.1新特性之四:p命名空间设置注入(待补充)
https://www.ibm.com/developerworks/cn/java/j-lo-jparelated/ http://www.ibm.com/developerworks/cn/jav ...
- 用C语言实现中文到unicode码的转换
转自: http://blog.csdn.net/qq_21792169/article/details/50379275 源文件用不同的编码方式编写,会导致执行结果不一样 由于本人喜欢用Notep ...
- NEKOGAMES
http://bbs.3dmgame.com/thread-4133434-1-1.html
- 为JFileChooser设定扩展名过滤
--------------------siwuxie095 工程名:TestFileChooser 包名:com.siwuxie095.fil ...
- Coding CTO 孙宇聪:《人,技术与流程》
我先做一下自我介绍,我是 07 年加入的 Google,在 Moutain View 总部任Google SRE,今年年初回国加入 Coding . 在 Google 我参与了两个 Project, ...
- 第5季-小试牛刀-项目开发\阶段2-新手上路\项目-移动物体监控系统\Sprint0-产品设计与规划
lesson1---产品功能展示 先完成准备阶段,准备阶段要做的事情: a.项目经理选择团队, b.根据项目用户需求以及同类型的实物,制定产品功能列表 c.根据功能的难易程度,制定迭代周期以及在每周期 ...
- CSS学习系列3--CSS3中的box-shadow的使用
在CSS中,text-shadow是给文本添加阴影效果. box-shadow则是给元素块添加周边阴影效果. box-shadow基本的语法形式如下 box-shadow: [inset] x-off ...
- sublime插件-OmniMarkupPreviewer浏览器打开报404解决办法
Sublime Text > Preferences > Package Settings > OmniMarkupPreviewer > Settings - Default ...