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 ...
随机推荐
- 人工蜂群算法-python实现
ABSIndividual.py import numpy as np import ObjFunction class ABSIndividual: ''' individual of artifi ...
- jQuery键盘事件绑定Enter键
<script> $(function(){ $(document).keydown(function(event){ if(event.keyCode==13){ $("#mo ...
- 从注册流程 分析如何安全退出多个Activity 多种方式(附DEMO)
退出Activity注册Android遍历 目录(?)[+] 前言 知识结构 具体方案 方案1 方法采用FLAG_ACTIVITY_CLEAR_TOP退出整个程序多activity 方案2 方 ...
- GNUPLOT画图工具
http://blog.csdn.net/codingkid/article/details/7211492 不得不说这个工具实在是太强大了. 1.首先命令简单,不会有那么多的语法问题. 2.其次画图 ...
- SpringMVC 2.5.6 noMapping
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- MyEclipse------缓冲流的使用
可以把BufferedReader和BufferedWriter称为上层流,把它们指向的字符流(Reader,Writer)称为底层流. Java采用缓存技术将上层流和底层流连接. 底层字符输入流先将 ...
- struts2基本配置
struts.xml 放在src目录下 <?xml version="1.0" encoding="UTF-8"?> <struts> ...
- 数据库的模糊查询mybatis
<!-- oracle --> <select id="searchUserBySearchName" parameterType="java.lang ...
- ssh项目删除
1.在页面添加删除跳转 <td><a href="deleteStandard.action?id=${sta.id }">删除</a>< ...
- JS 键值对
function Map() { this.keys = new Array(); this.data = new Array(); //添加键值对 this.set = function (key, ...