[LC] 32. Longest Valid Parentheses
2024-10-08 20:45:32
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"()()"
class Solution {
public int longestValidParentheses(String s) {
if (s == null || s.length() == 0) {
return 0;
} LinkedList<Integer> stack = new LinkedList<>();
int j = -1, res = 0;
for (int i = 0; i < s.length(); i++) {
char cur = s.charAt(i);
if (cur == '(') {
stack.offerFirst(i);
} else if (cur == ')' && stack.isEmpty()) {
j = i;
} else {
stack.pollFirst();
if (stack.isEmpty()) {
res = Math.max(res, i - j);
} else {
res = Math.max(res, i - stack.peekFirst());
}
}
}
return res;
}
}
最新文章
- Red Hat5下源码安装mysql5.6过程记录
- (转)nodejs中npm常用命令
- ajax里面success函数return上层接收不到
- PHP基于websocket实时通信的实现—GoEasy
- CF460 A. Vasya and Socks
- facebook-开发
- 在myeclipse10.7.1中写代码有很多红x
- apache下ab.exe使用方法。。
- C#中从元数据
- C#中KeyDown和KeyPress区别
- MFC解决Static控件背景透明时文本覆盖重影
- Redis 学习笔记五 经常使用php函数
- Apache配置虚拟文件夹
- H264中的MapUnits
- Entity Framework技巧系列之十一 - Tip 42 - 45
- ROS(indigo)swarm_robot 群机器人示例Gazebo
- Ali OSS服务端签名直传并设置上传回调
- RBMQ发布和订阅消息
- opencv+qt+vtk,编程时报错&#39;detail&#39;:ambiguous symbol
- 贝叶斯分类器,随机森林,梯度下载森林,神经网络相关参数的意义和data leakage
热门文章
- 2.0 虚拟机linu开启ssh服务与FTP
- cafe-ssd數據集訓練
- Maven - No plugin found for prefix &#39;tomcat7&#39; in the current project
- matlab 高级
- 201409-1 相邻数对 Java
- [Shoi2013]超级跳马(DP+矩阵乘法)
- linux笔记(一)——基本命令和快捷键
- Java使用Thrift,Thrift结构体定义
- $n$阶常微分方程通解中常数独立的意义
- http跳转https反向代理配置