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.

解题思路:

本题方法多多,是Java for LeetCode 020 Valid Parentheses题目的延续,因此,我们继续用栈的思路解决这个问题,其中需要一个index来维护最后一个')'出现的位置。JAVA代码如下:

static public int longestValidParentheses(String s) {
int maxLength = 0,index=-1;
Stack<Integer> stack = new Stack<Integer>();
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '(')
stack.push(i);
else {
if(stack.empty())
index=i;
else{
stack.pop();
if(stack.isEmpty())
maxLength = Math.max(maxLength, i - index);
else maxLength=Math.max(maxLength, i - stack.peek());
}
}
}
return maxLength;
}

Java for LeetCode 032 Longest Valid Parentheses的更多相关文章

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

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

  2. LeetCode 032 Longest Valid Parentheses

    题目描述:Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the l ...

  3. Java [leetcode 32]Longest Valid Parentheses

    题目描述: Given a string containing just the characters '(' and ')', find the length of the longest vali ...

  4. LeetCode 之 Longest Valid Parentheses(栈)

    [问题描写叙述] Given a string containing just the characters '(' and ')', find the length of the longest v ...

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

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

  6. leetcode 32. Longest Valid Parentheses

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

  7. 【leetcode】Longest Valid Parentheses

    Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...

  8. 【leetcode】 Longest Valid Parentheses (hard)★

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

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

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

随机推荐

  1. Android4.4中不能发送SD卡就绪广播

    当在Android上进行图片的扫描功能开发时一般会使用:sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse(“file:// ...

  2. java2集合框架的一些个人分析和理解

    Java2中的集合框架是广为人知的,本文打算从几个方面来说说自己对这个框架的理解. 下图是java.util.Collection的类图(基本完整,有些接口如集合类均实现的Cloneable.Seri ...

  3. 学习笔记-动态树Link-Cut-Tree

    --少年你有梦想吗? --少年你听说过安利吗? 安利一个集训队讲解:http://wenku.baidu.com/view/75906f160b4e767f5acfcedb 关于动态树问题,有多种方法 ...

  4. spring获取ApplicationContext对象的方法——ApplicationContextAware

    一. 引言 工作之余,在看一下当年学的spring时,感觉我们以前都是通过get~ set~方法去取spring的Ioc取bean,今天就想能不能换种模型呢?因为我们在整合s2sh时,也许有那么一天就 ...

  5. Java JDBC下执行SQL的不同方式、参数化预编译防御

    相关学习资料 http://zh.wikipedia.org/wiki/Java数据库连接 http://lavasoft.blog.51cto.com/62575/20588 http://blog ...

  6. TCP/IP详解 笔记八

    UDP协议 UDP是传输层协议,提供无连接不可靠的数据传输,其优点失效率高,确定确定是无序不可靠. 报文格式 UDP头部 TCP和UDP的端口号是独立的 UDP长度是指UDP数据报的总长度 UDP的校 ...

  7. Jquery-easyUI-datagrid参数之 queryParams

    http://blog.163.com/xpf_designer/blog/static/19213618920117784055668/ Html <div  region="cen ...

  8. Java中数据类型及其之间的转换

    Java中数据类型及其之间的转换 基本的数据类型 基本类型有以下四种:1)int长度数据类型有:byte(8bits).short(16bits).int(32bits).long(64bits).2 ...

  9. SSH协议及其应用

    SSH协议及其应用 原文作者:阮一峰 链接: http://www.ruanyifeng.com/blog/2011/12/ssh_remote_login.html http://www.ruany ...

  10. FCKeditor漏洞利用

    FCKeditor漏洞利用 查看编辑器版本 FCKeditor/_whatsnew.html fckeditor/editor/dialog/fck_about.html —————————————— ...