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 二.我的做题方法 简单理解了一下,用栈就可以实现.实际上是我考虑简单了,经过 ...
随机推荐
- 【原】js 签到用日历
最近做的一个项目中,需要用到一个日历来记录你的签到,网上找了一些,感觉挺庞大的,所以就自己写了一个,记录一下自己写这个日历的经过 html代码: <table cellspacing=" ...
- SMO推导和代码-记录毕业论文4
SMO的数学公式通过Platt的论文和看这个博客:http://www.cnblogs.com/jerrylead/archive/2011/03/18/1988419.html,大概弄懂了.推导以后 ...
- python设计模式之--单例模式
python的单例模式就是一个类的实例只能自始自终自能创建一次.应用场景比如说数据库的连接池. #!/usr/bin/env python # coding=utf- class Foo(object ...
- awk sed
awk -F '|' '{print $3","$4","$5}' cred.ts | sed 's/-//g'
- 使用ASP.NET Web Api构建基于REST风格的服务实战系列教程【三】——Web Api入门
系列导航地址http://www.cnblogs.com/fzrain/p/3490137.html 前言 经过前2节的介绍,我们已经把数据访问层搭建好了,从本章开始就是Web Api部分了.在正式开 ...
- ecshop 后台【左侧新增菜单】
模板文件admin/template/menu.htm admin/includes/inc_menu.php 菜单排序(链接) langagues/zh_cn/admin/common.php 语 ...
- javascript函数的定义与执行
要理解javascript函数的定义与执行,首先需要知道这几个重要的概念,现在可以先知道稍后再理解! 函数的执行环境(excution context).活动对象(call object).作用域(s ...
- RabbitMQ安装后不能运行 Error: unable to connect to node nodedown
本地安装RabbitMQ后总是不能正常的使用.. 命令行输入 rabbitMQctl Status 报下边的错 Error: unable to connect to node 'rabbit@YO ...
- inline-block元素overflow:hidden对齐问题
inline-block元素设置overflow:hidden后,其本身会上移 解决方法:在该元素或其父元素上设置vertical-align:bottom 原因解释:inline-block元素被设 ...
- Python开发【第十七篇】:MySQL(一)
一.概述 1.什么是数据库 ? 答:数据的仓库,如:在ATM的示例中我们创建了一个 db 目录,称其为数据库 2.什么是 MySQL.Oracle.SQLite.Access.MS SQL Serve ...