Java for LeetCode 032 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.
解题思路:
本题方法多多,是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的更多相关文章
- [LeetCode] 032. Longest Valid Parentheses (Hard) (C++)
指数:[LeetCode] Leetcode 指标解释 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 032. Lon ...
- LeetCode 032 Longest Valid Parentheses
题目描述:Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the l ...
- Java [leetcode 32]Longest Valid Parentheses
题目描述: Given a string containing just the characters '(' and ')', find the length of the longest vali ...
- LeetCode 之 Longest Valid Parentheses(栈)
[问题描写叙述] Given a string containing just the characters '(' and ')', find the length of the longest v ...
- [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
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- 【leetcode】Longest Valid Parentheses
Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...
- 【leetcode】 Longest Valid Parentheses (hard)★
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- [leetcode]32. Longest Valid Parentheses最长合法括号子串
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
随机推荐
- Java获取各种常用时间方法大全
Java获取各种常用时间方法大全 package cc.javaweb.test; Java中文网,Java获取各种时间大全 import java.text.DateFormat; import j ...
- WebLogic10安装图文教程
一 WebLogic安装 1. 打开WebLogic安装程序:oepe11_wls1031.exe(我们选用的是WebLogic 10.3g).如图1-1所示: 2. 进入WebLogic安装的欢迎 ...
- easyVS
easyVS 详细说明点这里
- Dedecms include\dialog\select_soft_post.php Upload Any Files To The Specified Directory Via Variable Not Initial Flaw Bypass Extension Defence
目录 . 漏洞描述 . 漏洞触发条件 . 漏洞影响范围 . 漏洞代码分析 . 防御方法 . 攻防思考 1. 漏洞描述 综合来说,这个漏洞的根源是"register_globals = on& ...
- python二维数组
和c c++不一样 过程如下: #-*- coding:utf-8 -*- t = [[ 0 for i in range(5)]for j in range(5)] for i in range(5 ...
- Opencv加载和显示图片
#include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <ios ...
- codeforce626D (概率)
D. Jerry's Protest time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- springmvc常用注解标签详解
1.@Controller 在SpringMVC 中,控制器Controller 负责处理由DispatcherServlet 分发的请求,它把用户请求的数据经过业务处理层处理之后封装成一个Model ...
- jquery------提供灵活的方法参数
index.jsp <h1 >再次重逢的世界</h1> my.js $(document).ready(function(){ (function($){ $.fn.shado ...
- GTP V0 和 GTP V1
GTP概述 GTP(GPRS Tunnelling Protocol)协议应用在SGSN 和GGSN 之间,为各个移动台(MS) 建立GTP 通道,GTP 通道是 GPRS服务节点(GSN) 之间的安 ...