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 ...
随机推荐
- maven-相关配置
Linux Ubuntu 安装Maven 我配置了 sudo gedit /etc/profile 配置了vi .bashrc 这个文档比较好(参考文档:http://www.linuxidc.co ...
- POJ 2182 Lost Cows
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10996 Accepted: 7059 Description N (2 ...
- html,body { margin:0; padding:0;border:0}
body,html /* 设置窗口DIV为浏览器大小*/ { margin:; padding:; height:100%; } 下面代码 <!DOCTYPE html> <html ...
- wifi与wimax
这几天在看文献中看到802.11a,802.11n和802.16e这几种无信通信协议标准,在网上查了相关资料后,看到有个帖子总结得不错,故将其转载过来. 转:http://blog.csdn.net/ ...
- udp内网穿透 两个内网互联
1,在有外网ip的机器上启动server. package udp; import java.net.DatagramPacket; import java.net.InetSocketAddress ...
- Activity的成员变量
// set by the thread after the constructor and before onCreate(Bundle savedInstanceState) is called. ...
- LESSCSS的几点摘要
字符串插值 变量可以用像 @{name} 这样的结构,以类似 ruby 和 php 的方式嵌入到字符串中: @base-url: "http://assets.fnord.com" ...
- ASP.NET MVC 4 跨域
<system.webServer> <httpProtocol> <customHeaders> <add name="Access-Contro ...
- Jmeter安装使用教程
解压jakarta-jmeter-X.X.zip至至如c盘:C:/jakarta-jmeter-2.2目录下. 桌面上选择“我的电脑”(右键),高级, 环境变量, 在“系统变量”---> ...
- yum只下载而不安装软件包?
yum本身自带了两个选项, 用来只下载要安装的rpm包, 而并不实际安装包: yum --downloadonly --downloaddir=/root/Desktop rpm-name1 rpm ...