32. Longest Valid Parentheses
来源:互联网 发布:nginx怎么使用 编辑:程序博客网 时间:2024/05/19 14:54
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.
这种最长、最短字符串的处理,一般都可以采用滑动窗口的方法实现,程序如下:
class Solution { public int longestValidParentheses(String s) { int len = s.length(); ArrayDeque<Integer> stack = new ArrayDeque<>(); int cnt = 0, length = 0, left = -1; for (int i = 0; i < len; ++ i){ if (s.charAt(i) == '('){ stack.push(i); continue; } if (stack.isEmpty()){ left = i; } else { stack.pop(); if (stack.isEmpty()){ length = Math.max(length, i - left); } else { length = Math.max(length, i - stack.peek()); } } } return length; }}
阅读全文
0 0
- 32.Longest Valid Parentheses
- 32. Longest Valid Parentheses
- 32. Longest Valid Parentheses
- 32. Longest Valid Parentheses
- 32. Longest Valid Parentheses
- 32. Longest Valid Parentheses
- 32. Longest Valid Parentheses
- 32. Longest Valid Parentheses
- 32. Longest Valid Parentheses
- 32. Longest Valid Parentheses
- 32. Longest Valid Parentheses
- 32. Longest Valid Parentheses
- 32. Longest Valid Parentheses
- 32. Longest Valid Parentheses
- 32. Longest Valid Parentheses
- 32. Longest Valid Parentheses
- 32. Longest Valid Parentheses
- 32. Longest Valid Parentheses
- C++ 友元friend的使用
- K.N King 现代方法 p25_8(计算还贷剩余)
- 《Java核心技术 卷I》笔记 第一章 Java程序设计概述
- 记录我的Java学习经历(9)
- bzoj1349 [Baltic2006]Squint
- 32. Longest Valid Parentheses
- Focal Loss for Dense Object Detection
- 出现Server Tomcat v7.0 Server at localhost failed to start的解决方法
- (转)情商高就是让别人舒服?呵呵
- js中原型,原型链,原型链继承的个人理解
- ubuntu系统无法访问磁盘解决办法
- 简理财COO郭勇:大数据驱动线上财富管理的深层逻辑是什么?
- 常见分布式系统数据分布解析
- ubuntu16.04安装配置mpich2