leetcode — longest-valid-parentheses
import java.util.Stack;
/**
* Source : https://oj.leetcode.com/problems/longest-valid-parentheses/
*
* Created by lverpeng on 2017/7/13.
*
* 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.
*
*/
public class LongestParentheses {
/**
* 最长有效的括号字符串
*
* 和之前判断括号匹配的一样使用栈,因为要计算长度,所以栈里面放括号字符的序号
*
* 边界情况:
* 栈为空遇到了右括号,开始压入一个-1,遇到右括号换的时候出栈,并压入新的右括号的序号
*
* @param parentheseStr
* @return
*/
public int longestParentheses (String parentheseStr) {
Stack<Integer> stack = new Stack<Integer>();
int maxLen = 0;
stack.push(-1);
for (int i = 0; i < parentheseStr.length(); i++) {
if (parentheseStr.charAt(i) == '(') {
stack.push(i);
} else {
if (stack.size() > 1) {
stack.pop();
int temp = stack.peek();
if (maxLen < (i - temp)) {
maxLen = i - temp;
}
} else {
stack.pop();
stack.push(i);
}
}
}
return maxLen;
}
public static void main(String[] args) {
LongestParentheses longestParentheses = new LongestParentheses();
System.out.println(longestParentheses.longestParentheses("(()"));
System.out.println(longestParentheses.longestParentheses(")()())"));
}
}
leetcode — longest-valid-parentheses的更多相关文章
- [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 ...
- [Leetcode] longest valid parentheses 最长的有效括号
Given a string containing just the characters'('and')', find the length of the longest valid (well-f ...
- [LeetCode] Longest Valid Parentheses
第一种方法,用栈实现,最容易想到,也比较容易实现,每次碰到‘)’时update max_len,由于要保存之前的‘(’的index,所以space complexity 是O(n) // 使用栈,时间 ...
- [LeetCode] 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 -- 挂动态规划羊头卖stack的狗肉
(Version 1.3) 这题在LeetCode上的标签比较有欺骗性,虽然标签写着有DP,但是实际上根本不需要使用动态规划,相反的,使用动态规划反而会在LeetCode OJ上面超时.这题正确的做法 ...
- leetcode: Longest Valid Parentheses分析和实现
题目大意:给出一个只包含字符'('和')'的字符串S,求最长有效括号序列的长度. 很有趣的题目,有助于我们对这种人类自身制定的规则的深入理解,可能我们大多数人都从没有真正理解过怎样一个括号序列是有效的 ...
- leetcode Longest Valid Parentheses python
class Solution(object): def longestValidParentheses(self, s): """ :type s: str :rtype ...
- Java for LeetCode 032 Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
随机推荐
- JavaScript中Object.keys用法
Object.keys()方法会返回一个由一个给定对象的自身可枚举属性组成的数组. var data={a:1,b:2,c:9,d:4,e:5}; console.log(data);//{a: 1, ...
- HTML之 一 标签
一 ,标签分类: 1.普通标签: <h1> hello </h1> hello 2.自闭和标签 <hr/> 二,书写html注意事项 1.标签不能交叉嵌套 2. 标 ...
- pycharm License server激活
2018-11-15 pycharm License server激活有效:https://idea.ouyanglol.com/
- [小结] 中山纪念中学2018暑期训练小结(划掉)(颓废记)-Day10
[小结] 中山纪念中学2018暑期训练小结(划掉)(颓废记)-Day10 各位看众朋友们,你们好,今天是2018年08月14日,星期二,农历七月初四,欢迎阅看今天的颓废联编节目 最近发生的灵异事件有 ...
- hadoop2.7单节点
$ sudo apt-get install ssh$ sudo apt-get install rsync 修改文件 etc/hadoop/hadoop-env.sh # set to the ro ...
- Debian 9.x "stretch" 安装 vnStat 统计服务器流量
vnStat 是一款开源的 Linux 下统计网卡流量的软件,可以很方便地查看当前.当天.当月的流量统计报告,下面我们介绍下在 Debian 9.x 下安装 vnstat 的简单方法 首先,使用 ip ...
- 多级路由请求js文件路径不对的解决方法
1.问题描述 最近因为项目的原因开始学习vue,看了几天教程然后开始撸项目.撸的过程也挺顺利,撸了一个多月项目要上线的时候却出现了问题——用history模式打开网站的时候,从导航点到具体的内容页是正 ...
- 28.实现 strStr() 函数
28.实现 strStr() 函数 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始).如果不存在, ...
- 微软SSAS 错误 Internal error: Invalid enumeration value. Please call customer support! 不是此元素的有效值。
错误 118 Internal error: Invalid enumeration value. Please call customer support! 不是此元素的有效值. 错误 119 分析 ...
- ETC(电子不停车收费系统)的发展演变
ETC引进中国是在上世纪的90年代中期,当时中国部分经济发达地区的高速公路车流量激增,从而导致了收费口的交通堵塞.高速公路堵车现象时有发生,拥堵严重的路段可能会天天堵,有时候一堵好几天.高速公路管理手 ...