[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 ...
随机推荐
- oracle-taf
http://blog.sina.com.cn/s/blog_48567d850102wck0.html配置目标:把RAC系统配置为“主-备”模式,即平时所有连接都在rac01这个节点上,当rac01 ...
- Spring事务原理
Spring事务的本质是对数据库事务的封装支持,没有数据库对事务的支持,Spring本身无法提供事务管理功能.对于用JDBC操作数据库想要用到事务,必须经过获取连接——>开启事务——>执行 ...
- [JAVA]JAVA多线程实现方法之——实现Runnable接口
public class MultiThread { public static void main(String[] args) { Thread t1 = new Thread(new Threa ...
- sublime text 3 vue 语法高亮
1.下载文件 链接 https://github.com/vuejs/vue-syntax-highlight 2.sublime菜单栏->Preferences->Browse Pack ...
- shutil 拷贝 / 移动 / 压缩 / 解压缩
# shutil_demo.py 高级文件操作(拷贝 / 移动 / 压缩 / 解压缩) import shutil def shutil_demo(): # 拷贝文件 shutil.copy2('fi ...
- Linux系统目录权限chmod误操作权限修复方法
Linux中,如果意外误操作将/目录权限批量设置,比如chmod -R 777 / ,系统中的大部分服务以及命令将无法使用,这时候可以通过系统自带的getfacl命令来拷贝和还原系统权限,若是其他系统 ...
- 记一次sql server 2005访问http接口,并解析json的过程
记一次sql server 2005访问http接口,并解析json的过程 JSON解析官方网站:https://www.red-gate.com/simple-talk/sql/t-sql-pro ...
- 【剑指offer】链表第一个公共子结点
*思路: 先求得两个链表的长度,然后得到长度差diff,再先遍历长链表diff步后,再同时遍历两个链表并比较对象指针. /* public class ListNode { int val; List ...
- windows下缩短time_wait的时间
最近线上遇到windows机器访问其他机器的时候失败的情况.实际就是本地的端口不够用造成的. D:\>netsh interface ipv4 show dynamicportrange pro ...
- 简单的知识图谱,neo4j+python
因为研究方向是知识图谱,就有兴致想要构建一个简单的知识图谱,就在网上查找了一下,参考了neo4j搭建简单的金融知识图谱的思想,就着手从零开始构建. 1.首先就要考虑数据的获得,因为之前没有接触过爬虫之 ...