题目描述:

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. WPF ListBox的DataTemplate例子

    <ListBox Name="lbLogInfo"> <ListBox.ItemTemplate> <DataTemplate> <Gri ...

  2. 1006. Sign In and Sign Out

    #include <stdio.h> #include <algorithm> #include <iostream> #include <string.h& ...

  3. Fedora 命令

    1. 更新包 yum clear all yum -y update 2.yum包查找 yum whatprovides xxxx.os.l 3 df 查看磁盘空间 xclip 复制到粘贴板 xcli ...

  4. (转)《深入理解java虚拟机》学习笔记8——Tomcat类加载器体系结构

    Tomcat 等主流Web服务器为了实现下面的基本功能,都实现了不止一个自定义的类加载器: (1).部署在同一个服务器上的两个web应用程序所使用的java类库可以相互隔离. (2).部署在同一个服务 ...

  5. centos6.5安装配置LDAP服务[转]

    安装之前查一下 1 find / -name openldap* centos6.4默认安装了LDAP,但没有装ldap-server和ldap-client 于是yum安装 1 su root 2 ...

  6. HubbleDotNet开源全文搜索组件相关资源

    系统简介 HubbleDotNet 是一个基于.net framework 的开源免费的全文搜索数据库组件.开源协议是 Apache 2.0.HubbleDotNet提供了基于SQL的全文检索接口,使 ...

  7. redis缓存的安装和使用

    Redis介绍    Redis本质上一个Key/Value数据库,与Memcached类似的NoSQL型数据库,但是他的数据可以持久化的保存在磁盘上,解决了服务重启后数据不丢失的问题,他的值可以是s ...

  8. Delphi与Qt在Windows下使用共享内存进程间通信

    Delphi部分 type  TGuardInfo=record    Lock: Integer;  end;  PGuardInfo = ^TGuardInfo; TGuardShareMem=c ...

  9. 进入 App Store 打分

    很多用户用了好软件后忘记或嫌麻烦而不去 App Store 进行打分评星,为此开发者可以在应用中加入打分按钮,点击后直接跳转到 App Store 的评分界面. App Store 上评论的链接地址是 ...

  10. struts2+hibernate-jpa+Spring+maven 整合(2)

    1.修改pom.xml 1. 添加  slf4j-api <dependency> <groupId>org.slf4j</groupId> <artifac ...