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;
}
}

最新文章

  1. Red Hat5下源码安装mysql5.6过程记录
  2. (转)nodejs中npm常用命令
  3. ajax里面success函数return上层接收不到
  4. PHP基于websocket实时通信的实现—GoEasy
  5. CF460 A. Vasya and Socks
  6. facebook-开发
  7. 在myeclipse10.7.1中写代码有很多红x
  8. apache下ab.exe使用方法。。
  9. C#中从元数据
  10. C#中KeyDown和KeyPress区别
  11. MFC解决Static控件背景透明时文本覆盖重影
  12. Redis 学习笔记五 经常使用php函数
  13. Apache配置虚拟文件夹
  14. H264中的MapUnits
  15. Entity Framework技巧系列之十一 - Tip 42 - 45
  16. ROS(indigo)swarm_robot 群机器人示例Gazebo
  17. Ali OSS服务端签名直传并设置上传回调
  18. RBMQ发布和订阅消息
  19. opencv+qt+vtk,编程时报错&#39;detail&#39;:ambiguous symbol
  20. 贝叶斯分类器,随机森林,梯度下载森林,神经网络相关参数的意义和data leakage

热门文章

  1. 2.0 虚拟机linu开启ssh服务与FTP
  2. cafe-ssd數據集訓練
  3. Maven - No plugin found for prefix &#39;tomcat7&#39; in the current project
  4. matlab 高级
  5. 201409-1 相邻数对 Java
  6. [Shoi2013]超级跳马(DP+矩阵乘法)
  7. linux笔记(一)——基本命令和快捷键
  8. Java使用Thrift,Thrift结构体定义
  9. $n$阶常微分方程通解中常数独立的意义
  10. http跳转https反向代理配置