题目描述:

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.

解题思路:

基本的想法就是用栈来实现。从字符串的第一个位置开始读起,遇到左括号的话就入栈,遇到右括号的话就将栈顶元素弹出,并且判断当前序列的最长长度。

栈里保存的是左括号的位置。

具体遇到右括号时,有如下几种情况需要考虑:

1. 当前栈内无元素,则用start变量记录当前右括号的位置,表明下次比较从该右括号下一个位置开始判断;

2. 当前栈内有元素,则将栈顶元素弹出。如果弹出后的栈为空,则表明当前括号匹配,这时计算出当前的最长序列长度,即当前位置的值减去start的值即是;

3. 当前栈内又元素且弹出后的栈内仍然有元素,则最长序列长度为当前元素位置减去栈顶元素的位置。

本算法仅需要扫描一遍字符串,所以时间复杂度为O(n);

代码如下:

public class Solution {
public int longestValidParentheses(String s) {
ArrayList<Integer> location = new ArrayList<Integer>();
int len = 0;
int start = -1;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '(') {
location.add(i);
} else {
if (location.isEmpty()) {
start = i;
} else {
location.remove(location.size() - 1);
if (location.isEmpty()) {
len = Math.max(len, i - start);
} else {
int index = location.get(location.size() - 1);
len = Math.max(len, i - index);
}
}
}
}
return len;
}
}

Java [leetcode 32]Longest Valid Parentheses的更多相关文章

  1. [LeetCode] 32. Longest Valid Parentheses 最长有效括号

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  2. leetcode 32. Longest Valid Parentheses

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  3. [leetcode]32. Longest Valid Parentheses最长合法括号子串

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  4. LeetCode 32 Longest Valid Parentheses(最长合法的括号组合)

    题目链接: https://leetcode.com/problems/longest-valid-parentheses/?tab=Description   Problem :已知字符串s,求出其 ...

  5. [LeetCode] 32. Longest Valid Parentheses (hard)

    原题链接 题意: 寻找配对的(),并且返回最长可成功配对长度. 思路 配对的()必须是连续的,比如()((),最长长度为2:()(),最长长度为4. 解法一 dp: 利用dp记录以s[i]为终点时,最 ...

  6. [Leetcode][Python]32: Longest Valid Parentheses

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 32: Longest Valid Parentheseshttps://oj ...

  7. leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、

    20. Valid Parentheses 错误解法: "[])"就会报错,没考虑到出现')'.']'.'}'时,stack为空的情况,这种情况也无法匹配 class Soluti ...

  8. [LeetCode] 032. Longest Valid Parentheses (Hard) (C++)

    指数:[LeetCode] Leetcode 指标解释 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 032. Lon ...

  9. 刷题32. Longest Valid Parentheses

    一.题目说明 题目是32. Longest Valid Parentheses,求最大匹配的括号长度.题目的难度是Hard 二.我的做题方法 简单理解了一下,用栈就可以实现.实际上是我考虑简单了,经过 ...

随机推荐

  1. C语言-创建链表及排序

    #include <stdio.h> #define NEWNODE (Node *)malloc(sizeof(Node)) typedef struct mynode{ int num ...

  2. Sqoop 1.99.4 安装

    1.安装准备工作:已经装好的 hadoop 环境是 hadoop-2.5.1 64位下载的sqoop安装包(注意是hadoop200)http://www.us.apache.org/dist/sqo ...

  3. Java类加载器加载类顺序

    java ClassLoader的学习 java是一门解释执行的语言,由开发人员编写好的java源文件先编译成字节码文件.class形式,然后由java虚拟机(JVM)解释执 行,.class字节码文 ...

  4. MINA快速传输文件

    最近的项目使用MNA进行文件传输,只能传输到5~7MB/s:但是使用FTP等软件其实可以达到11MB/s,后来使用MINA原生传输,发现可以达到11MB/s,后来发现有以下两点可以需要注意优化: 1. ...

  5. poj 1236 Network of Schools(又是强连通分量+缩点)

    http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Su ...

  6. shell复习笔记----查找与替换

    查找文档 以grep 程序查找文本(匹配文本 matching text)相当方便.传统上有三种程序可以用来查找整个文本文件. grep 最早的文本匹配程序.其最简单的方式就是使用固定字符串 $ wh ...

  7. 读书笔记 (二) ———Fundamentals of Multiagent Systems with NetLogo Examples by Prof. Jose M Vidal

    chapter 2 分布式约束1 分布式约束满足 1.1 过滤算法 1.2 基于归结的调和算法 consistency 1.3 异步回溯 1.4 异步弱承诺? 1.5 分布式突破?2 分布式受限优化 ...

  8. 在SpringMVC利用MockMvc进行单元测试

    spring在线文档:https://docs.spring.io/spring/docs/current/javadoc-api/index.html?index-files/index-13.ht ...

  9. js中批量处理样式——cssText的使用

    http://www.cnblogs.com/snandy/archive/2011/03/12/1980444.html

  10. Maven Source jar

    http://blog.csdn.net/symgdwyh/article/details/4407945