Longest Valid Parentheses leetcode java
题目:
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.
题解:
这道题是求最长匹配括号的长度,而不是个数,我最开始想错了,去写个数去了。。。
题解引用自:http://codeganker.blogspot.com/2014/03/longest-valid-parentheses-leetcode.html
这道题是求最长的括号序列,比较容易想到用栈这个数据结构。基本思路就是维护一个栈,遇到左括号就进栈,遇到右括号则出栈,并且判断当前合法序列是否为最
长序列。不过这道题看似思路简单,但是有许多比较刁钻的测试集。具体来说,主要问题就是遇到右括号时如何判断当前的合法序列的长度。比较健壮的方式如下:
(1) 如果当前栈为空,则说明加上当前右括号没有合法序列(有也是之前判断过的);
(2)
否则弹出栈顶元素,如果弹出后栈为空,则说明当前括号匹配,我们会维护一个合法开始的起点start,合法序列的长度即为当前元素的位置
-start+1;否则如果栈内仍有元素,则当前合法序列的长度为当前栈顶元素的位置下一位到当前元素的距离,因为栈顶元素后面的括号对肯定是合法的,而
且左括号出过栈了。
因为只需要一遍扫描,算法的时间复杂度是O(n),空间复杂度是栈的空间,最坏情况是都是左括号,所以是O(n)。
然后网上找了解法,有些人使用了DP,我没仔细看。。主要还是吸取了栈的思想。代码引用自:http://rleetcode.blogspot.com/2014/01/longest-valid-parentheses.html, 个人稍作修改。
如下所示:
1 public static int longestValidParentheses(String s) {
2 if (s==null||s.length()==0){
3 return 0;
4 }
5
6 int start=0;
7 int maxLen=0;
8 Stack<Integer> stack=new Stack<Integer>();
9
for (int i=0; i<s.length();i++){
if (s.charAt(i)=='('){
stack.push(i);
}else{
if (stack.isEmpty()){
// record the position before first left parenthesis
start=i+1;
}else{
stack.pop();
// if stack is empty mean the positon before the valid left parenthesis is "last"
if (stack.isEmpty()){
maxLen=Math.max(i-start+1, maxLen);
}
else{
// if stack is not empty, then for current i the longest valid parenthesis length is
// i-stack.peek()
maxLen=Math.max(i-stack.peek(),maxLen);
}
}
}
}
return maxLen;
}
Longest Valid Parentheses leetcode java的更多相关文章
- Longest Valid Parentheses - LeetCode
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- Valid Parentheses leetcode java
题目: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the ...
- Java for LeetCode 032 Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- Java [leetcode 32]Longest Valid Parentheses
题目描述: Given a string containing just the characters '(' and ')', find the length of the longest vali ...
- [LeetCode] 032. Longest Valid Parentheses (Hard) (C++)
指数:[LeetCode] Leetcode 指标解释 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 032. Lon ...
- LeetCode 笔记系列八 Longest Valid Parentheses [lich你又想多了]
题目:Given a string containing just the characters '(' and ')', find the length of the longest valid ( ...
- LeetCode 32. 最长有效括号(Longest Valid Parentheses) 31
32. 最长有效括号 32. Longest Valid Parentheses 题目描述 给定一个只包含 '(' 和 ')' 的字符串,找出最长的包含有效括号的子串的长度. 每日一算法2019/6/ ...
- [LeetCode] Longest Valid Parentheses 最长有效括号
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- [LeetCode] Longest Valid Parentheses 解题思路
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
随机推荐
- yum安装(sentos7)
之前我的yum一直出问题,我就重装了yum,这个教程是我亲自测试过,有用的. 链接:http://blog.csdn.net/iamhuanggua/article/details/60140867 ...
- Music in Car CF 746F
题目:http://codeforces.com/problemset/problem/746/F 先感叹一下题目之长! 一些测试样例在后面给出. 题目大意: Sasha 去工作的路上喜欢听歌,途中经 ...
- 理解Linux的进程,线程,PID,LWP,TID,TGID
在Linux的top和ps命令中,默认看到最多的是pid (process ID),也许你也能看到lwp (thread ID)和tgid (thread group ID for the threa ...
- 【基础知识】.Net基础加强 第四天
一. 显示实现接口 1. 显示实现接口的目的:为了解决法方法重名的问题. 2. 显示实现接口必须是私有的,不能用public 3. (复习)类中成员不写访问修饰符默认是private:类如果不写访问修 ...
- 机器学习之路: tensorflow 自定义 损失函数
git: https://github.com/linyi0604/MachineLearning/tree/master/07_tensorflow/ import tensorflow as tf ...
- [BZOJ5287][HNOI2018]毒瘤(虚树DP)
暴力枚举非树边取值做DP可得75. 注意到每次枚举出一个容斥状态的时候,都要做大量重复操作. 建立虚树,预处理出虚树上两点间的转移系数.也可动态DP解决. 树上倍增.动态DP.虚树DP似乎是这种问题的 ...
- HTML5区块和大纲算法
原文链接: Using HTML sections and outlines - Mozilla Developer Network 每集HTML5+CSS3网页布局教程-2大纲算法 HTML5标准带 ...
- web开发中兼容性问题(IE8以上含)持续更新~~
在实际开发中总是遇到莫名其妙的问题~~~那么就记录下来这些问题,对这些问题进行一个总结. 1.事件对象 1)事件参数e,就是事件对象,标准的获取方式 2)e.eventPhase 事件阶段,IE8以前 ...
- php远程获取图片或文件信息(get_headers,fsocketopen,curl)
<?php if(!function_exists("remote_filesize")){ /** * 获取远程或本地文件信息 * @param string $strUr ...
- C# string byte[] Base64 常用互相转换
参考: http://www.cnblogs.com/zxx193/p/3605238.html?utm_source=tuicool http://www.cnblogs.com/freeliver ...