题目描述:

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. 1035 Password (20)

    #include <stdio.h> #include <string.h> struct MyStruct { ]; ]; bool changed; }; int main ...

  2. MySQL 5.1.63 单机配置多实例(简单配置)

    需求: 在一台服务器上通过源码编译安装一个版本为5.1.63版本MySQL数据库: 方案:将所有配置文件与数据等均存放在/home/zhaoshuangshuang下.在同一个MySQL中运行两个实例 ...

  3. Clone中存在的浅克隆问题

    A.java package second; public class A { String country;//国家 String province;//地区 String city;//城市 pu ...

  4. 【CSS】盒模型+选择器(你选择的要操作的对象)

    盒模型 转http://www.cnblogs.com/cchyao/archive/2010/07/12/1775846.html 1.w3c标准的盒模型和ie的盒模型主要差别在于content的w ...

  5. python学习笔记9(对文件的操作)

    一.文件对象 我理解的文件对象就是一个接口,通过这个接口对文件进行相关操作. 二.相关函数 [1].内建函数:open() 提供了初始化输入/输出(I/O)操作的通用接口,成功打开一个文件后会返回一个 ...

  6. (转)Linux 文件系统:procfs, sysfs, debugfs 用法简介

    网址:http://www.tinylab.org/show-the-usage-of-procfs-sysfs-debugfs/ 1 前言 内核中有三个常用的伪文件系统:procfs,debugfs ...

  7. Elasticsearch从0.90(0.90.x)到1.2(1.x)API的变化-二

    本文为官方文档的译文加个人理解.作者翻译时,elasticsearch(下面简称es)的版本为1.2.2. 请支持原创:http://www.cnblogs.com/donlianli/p/38367 ...

  8. poj 2553 The Bottom of a Graph(强连通分量+缩点)

    题目地址:http://poj.org/problem?id=2553 The Bottom of a Graph Time Limit: 3000MS   Memory Limit: 65536K ...

  9. embed标签遮住div层

    依然是上次的那个返工友情项目,当时帮忙用jquery ui写了一个漂浮的投票箱,就是类似点击一个项目然后就收藏到了投票箱中的效果.. 虽然不是很复杂,但是由于页面上有大面积的由kindeditor上传 ...

  10. 盒模型------CSS

    盒子的内心世界 1.模型 通过CSS的眼睛 在CSS看来,HTML的所有元素都被看成了盒子:段落,标题,块引用,列表,列表项等.甚至内联元素. 盒子的组成 内容区(content):包含内容(文本或图 ...