[leetcode]32. Longest Valid Parentheses最长合法括号子串
Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring.
Example 1:
Input: "(()"
Output: 2
Explanation: The longest valid parentheses substring is "()"
Example 2:
Input: ")()())"
Output: 4
Explanation: The longest valid parentheses substring is "()()"
题意:
给定一个括号序列,找出最长的合法括号子串。
Solution1: Two pointers(fast&slow) + Stack
It's just like using [start+1...i] to maintain a window where contains longest valid parentheses


code:
/*
Time: O(n). We traverse the given input
Space: O(n). Worse case, input is all contained '(', then there will be n chars stored in the stack
*/
class Solution {
public int longestValidParentheses(String s) {
int result = 0;
int start = -1;
Stack<Integer> stack = new Stack<>(); // index of every char for(int i = 0; i < s.length(); i++){
char c = s.charAt(i);
if(c == '('){
stack.push(i);
}else if( c == ')'){
if(stack.isEmpty()){
// no matching left '( '
start = i;
}else{
stack.pop();
if(stack.isEmpty()){
result = Math.max(result, i - start);
}else{
result = Math.max(result, i - stack.peek());
}
}
}
}
return result;
}
}
[leetcode]32. Longest Valid Parentheses最长合法括号子串的更多相关文章
- [LeetCode] 32. Longest Valid Parentheses 最长有效括号
		
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
 - 32. Longest Valid Parentheses最长有效括号
		
参考: 1. https://leetcode.com/problems/longest-valid-parentheses/solution/ 2. https://blog.csdn.net/ac ...
 - [LeetCode] 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 ...
 - Java [leetcode 32]Longest Valid Parentheses
		
题目描述: Given a string containing just the characters '(' and ')', find the length of the longest vali ...
 - 032 Longest Valid Parentheses 最长有效括号
		
给一个只包含 '(' 和 ')' 的字符串,找出最长的有效(正确关闭)括号子串的长度.对于 "(()",最长有效括号子串为 "()" ,它的长度是 2.另一个例 ...
 - LeetCode 32 Longest Valid Parentheses(最长合法的括号组合)
		
题目链接: https://leetcode.com/problems/longest-valid-parentheses/?tab=Description Problem :已知字符串s,求出其 ...
 - [LeetCode] 32. Longest Valid Parentheses (hard)
		
原题链接 题意: 寻找配对的(),并且返回最长可成功配对长度. 思路 配对的()必须是连续的,比如()((),最长长度为2:()(),最长长度为4. 解法一 dp: 利用dp记录以s[i]为终点时,最 ...
 - leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、
		
20. Valid Parentheses 错误解法: "[])"就会报错,没考虑到出现')'.']'.'}'时,stack为空的情况,这种情况也无法匹配 class Soluti ...
 
随机推荐
- docker  lamp
			
可以直接使用官方镜像搭建LAMP环境从官方下载PHP+Apache镜像和MySQL两个镜像来组成(如:php:7.2.3-apache-stretch和mysql:5.7.21)docker pull ...
 - mvc项目用log4net 记录错误日志
			
1. 首先下载lognet 下载地址 http://logging.apache.org/log4net/download_log4net.cgi 2.找到bin文件中的net文件夹 之后看你电脑 ...
 - Keepalived+LVS实现高可用负载均衡双主模式
			
LVS是一种集群(Cluster)技术:采用IP负载均衡技术和基于内容请求分发技术.调度器具有很好的吞吐率,将请求均衡地转移到不同的服务器上执行,且调度器自动屏蔽掉服务器的故障,从而将一组服务器构成一 ...
 - nginx里proxy_pass有无/的区别
			
nginx在反向代理的时候,proxy_pass需要指定路径,有无"/"的区别,如下: location /lile { 配置一: proxy_pass http://192. ...
 - shell 生成MAC地址
			
# cat /dev/urandom |od -x |awk '{print $2,$3,$4}' |head -n 1 |sed -e 's/[[:space:]]//g' -e 's/\(..\) ...
 - 关于各种BUF源语的研究
			
关于各种BUF源语的研究 资料来源: 单端信号需要用到的BUF 关于这些源语的约束: 增大驱动电流 关于管脚的上拉与下拉约束: ODDR的两种操作模式 关于ODDR输出时钟的应用 为什么ODDR需要这 ...
 - HBuilder后台保活开发(后台自动运行,定期记录定位数据)
			
http://ask.dcloud.net.cn/question/28090 后台自动运行,定期记录定位数据 分类:HTML5+ 各位新年好 小弟以前用hbuilder开发过几个项目,现在有一新 ...
 - HTTP响应过程
			
完整的一次 HTTP 请求响应过程(一)http://mp.weixin.qq.com/s?__biz=MzUzMTA2NTU2Ng==&mid=2247484648&idx=1&am ...
 - api.js封装请求
			
1. 传入对象格式如 { a:{ getData:{ url: 'xx/xx/xx', method: 'get', require:['id', 'name'], // 简单检查 必传参数确实则不发 ...
 - C语言 链表(VS2012版)
			
// ConsoleApplication2.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <stdio.h> ...