Longest Valid Parentheses 解答
Question
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.
Solution 1 -- Stack
Parenthese类的题目,第一反应是用栈。遇到'('入栈,遇到')'出栈。这题关键在于怎样知道当前的valid parenthese length。
我们用一个变量start来表示当前的第一个'(' 。
( ( ( ) ) ) ) ) (
start start
当栈为空时,进来的第一个左括号的位置为当前的start值。
出栈操作后:
1. 如果栈为空,说明从start开始的括号都匹配了。因此长度为 current - start + 1
2. 如果栈不为空,我们知道弹出栈的一定是都已经匹配了的。因此长度为 current - stack.peek()
因为只扫描了一遍原数组,所以时间复杂度是O(n),空间复杂度是O(n)
public class Solution {
public int longestValidParentheses(String s) {
if (s == null || s.length() < 1) {
return 0;
}
int length = s.length();
Deque<Integer> stack = new LinkedList<Integer>();
int start = 0;
int result = 0;
for (int i = 0; i < length; i++) {
char current = s.charAt(i);
if (current == '(') {
stack.push(i);
} else {
if (stack.isEmpty()) {
start = i + 1;
} else {
stack.pop();
result = stack.isEmpty() ? Math.max(result, i - start + 1) : Math.max(result, i - stack.peek());
}
}
}
return result;
}
}
Solution 2 -- DP
Longest Valid Parentheses 解答的更多相关文章
- LeetCode解题报告—— Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- [LeetCode] Longest Valid Parentheses 最长有效括号
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- 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 ...
- 【leetcode】Longest Valid Parentheses
Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...
- 【leetcode】 Longest Valid Parentheses (hard)★
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- Longest Valid Parentheses 每每一看到自己的这段没通过的辛酸代码
Longest Valid Parentheses My Submissions Question Solution Total Accepted: 47520 Total Submissions: ...
- [LeetCode] Longest Valid Parentheses 动态规划
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- Java for LeetCode 032 Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
随机推荐
- MSSQL的编译和执行过程
原文地址:http://www.cncms.com.cn/mssql/3350_11.htm
- Codeforce 215 div1
C 把每个qi看成点,则问题转化为:求一个最大的k,遍历k个点的完全图需要的最小步数+1不超过n, (这里+1的原因是把起点加进去) 讨论k的奇偶: k为奇数,每个点度数为偶数,这是一个欧拉回路,步数 ...
- Java中BigDecimal的8种舍入模式是怎样的
Java中BigDecimal的8种舍入模式是怎样的?下面长沙欧柏泰克软件学院和大家一起来学习下吧: java.math.BigDecimal 不可变的.任意精度的有符号十进制数.BigDecima ...
- 加密解密(2)*客户端,服务器,CA(Certificate Authority),公钥,私钥,证书,签名,验证
加密解密(2)*客户端,服务器,CA(Certificate Authority),公钥,私钥,证书,签名,验证 各角色比喻 客户端:通常为请求方,要验证服务器的身份. 服务器:通常为响应方,有时也要 ...
- HBase二级索引的设计(案例讲解)
摘要 最近做的一个项目涉及到了多条件的组合查询,数据存储用的是HBase,恰恰HBase对于这种场景的查询特别不给力,一般HBase的查询都是通过RowKey(要把多条件组合查询的字段都拼接在RowK ...
- Hacker(23)----破解常见文件密码
Win7中,office文档.压缩文件等都是常见的文件,这些文档含有重要的信息,即使用户为这些文件设置了密码,黑客也会有办法破解. 一.破解office文档密码 破解office文档密码常用工具是Ad ...
- Hacker(17)----认识Windows系统漏洞
Windows系统是迄今为止使用频率最高的操作系统,虽然其安全性随着版本的更新不断提高,但由于人为编写的缘故始终存在漏洞和缺陷.但Mircosoft公司通过发布漏洞补丁来提高系统的安全性,使Windo ...
- Error when launching Quest Central for DB2: "QCC10000E - Unable to allocate environment handle fo
标题 Error when launching Quest Central for DB2: "QCC10000E - Unable to allocate environment hand ...
- timed out waiting for input: auto-logout
The ssh "timed out waiting for input: auto-logout" messages is generated by ssh upon reach ...
- linux下安装软件的方法
1. 区分 rpm -qi -qf -ql -qa四个不同选项组合的作用?rpm -qi //查询已经安装的某个RPM软件包的信息rpm -qf //查询某个程序文件是由哪个RPM软件包安装的rpm ...