leetcode 32. Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring.
For "(()", the longest valid parentheses substring is "()", which has length = 2.
Another example is ")()())", where the longest valid parentheses substring is "()()", which has length = 4.
分析:
给出一组括号,找出其中连续有效括号的最长长度。
for循环遍历每个括号,当前位置为i,index用于记录每段有效括号的起始位置。
(1)判断当前位置是否是左括号,如果遇到左括号,入栈。
(2)如果不是左括号,
1,判断栈是否为空,如果为空,将index指针后移。
2,如果栈不为空,弹出栈顶元素。
此时,如果栈为空,加上当前位置i的右括号可以构成一段有效的括号,最长为max(maxLen, i到index之间的距离),
如果栈不为空,说明栈顶后一位开始到当前位置可以构成一段有效的括号,那最长为max(maxLen, i到栈顶后一位的长度)。
public class Solution {
public int longestValidParentheses(String s) {
if (s == null){
return 0;
}
Stack<Integer> p = new Stack<Integer>();
int maxLen = 0;
int index = 0;
for (int i = 0; i < s.length(); i++){
if (s.charAt(i) == '('){
p.push(i);
}else{
if(p.isEmpty()){
index = i + 1;
}else{
p.pop();
if(p.isEmpty()){
maxLen = Math.max(i - index + 1, maxLen);
}else{
maxLen = Math.max(i - p.peek(), maxLen);
}
}
}
}
return maxLen;
}
}
leetcode 32. Longest Valid Parentheses的更多相关文章
- [LeetCode] 32. Longest Valid Parentheses 最长有效括号
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- Java [leetcode 32]Longest Valid Parentheses
题目描述: Given a string containing just the characters '(' and ')', find the length of the longest vali ...
- [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(最长合法的括号组合)
题目链接: https://leetcode.com/problems/longest-valid-parentheses/?tab=Description Problem :已知字符串s,求出其 ...
- [LeetCode] 32. Longest Valid Parentheses (hard)
原题链接 题意: 寻找配对的(),并且返回最长可成功配对长度. 思路 配对的()必须是连续的,比如()((),最长长度为2:()(),最长长度为4. 解法一 dp: 利用dp记录以s[i]为终点时,最 ...
- [Leetcode][Python]32: Longest Valid Parentheses
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 32: Longest Valid Parentheseshttps://oj ...
- leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、
20. Valid Parentheses 错误解法: "[])"就会报错,没考虑到出现')'.']'.'}'时,stack为空的情况,这种情况也无法匹配 class Soluti ...
- [LeetCode] 032. Longest Valid Parentheses (Hard) (C++)
指数:[LeetCode] Leetcode 指标解释 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 032. Lon ...
- 刷题32. Longest Valid Parentheses
一.题目说明 题目是32. Longest Valid Parentheses,求最大匹配的括号长度.题目的难度是Hard 二.我的做题方法 简单理解了一下,用栈就可以实现.实际上是我考虑简单了,经过 ...
随机推荐
- MYSQL 常用函数(数学、字符串、日期时间、系统信息、加密)
一.数学函数 数学函数主要用于处理数字,包括整型.浮点数等. ABS(x) 返回x的绝对值 SELECT ABS(-1) -- 返回1 CEIL(x),CEILING(x) 返回大于或等于x的最小整数 ...
- C#------如何取出exe运行文件给客户使用
1.将解决方案配置里面的“Debug”转换成“Release” 2.右击“解决方案”,选着“重新生成解决方案”,以得到最新的版本 3.找到工程目录下的“bin”文件夹,里面有“Release”文件夹, ...
- 开源License
http://www.open-open.com/bbs/view/1319816219625 http://my.oschina.net/yangsheng/blog/190917
- 快捷键_Mac
苹果Mac系统常用快捷键 Command+Tab 任意情况下切换应用程序 - 向前循环 Shift+Command+Tab 切换应用程序 - 向后循环 Command+L 当前程序是浏览器时,可以直接 ...
- JS中call和apply
作用: 替换当前对象的方法中的this. 理解: call和apply是为了动态改变this出现的,当一个object没有某个方法,但是其他的有,我们可以借助call或apply用其它对象的方法来操作 ...
- memcache相同主域名下的session共享
本配置适合具有相同主域名的多台服务器进行session共享. 例如:www.lee.com , bbs.lee.com(多个子域名). 配置session保存在memcache: ini_set(&q ...
- heartbleed漏洞利用
1. heartbleed漏洞扫描: 2. heartbleed漏洞利用: poc.py 117.52.93.111 貌似没有打到管理员账号密码,可能是管理员没登录,其实,可以写一个自动 ...
- Sql2008R2设置远程链接
下边的文章是从百度经验里粘过来的.. 经过测试确实有效..留个备份.. 有个小情况在前边说一下.. 在操作前一定要确定自己的sa用户密码是不是一样..不要以为自己知道的是对的就直接略过某些步骤.. 俗 ...
- C# MVC EF中匿名类使用
控制器中代码: var list = context.Says.Join( context.Users, a => a.UserId, b => b.Id, (a, b) => ne ...
- MySQL里的found_row()与row_count()的解释及用法
MySQL中有两个函数来计算上一条语句影响了多少行,不同于SqlServer/Oracle,不要因为此方面的差异而引起功能问题 出处:mysqlpub.com MySQL中有两个函数来计算上一条语 ...