LeetCode: Longest Valid Parentheses 解题报告
Longest Valid Parentheses
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:
1. 使用栈来保存'('
2. tmp 表示当前计算的一套完整的括号集的长度。完整的指的是消耗掉栈中所有的'('.
3. sum 表示数个完整的括号集的总长。
例子:
有一套完整的括号集,可以加到前面的一整套括号集上
() (()())
1 2 第二套括号集可以加过来
4. 不完整的括号集:
这种情况也是需要计算的。也可能是一个未完成的括号集,比如:
() (()() 在这里 ()() 是一个未完成的括号集,可以独立出来计算,作为
阶段性的结果
5. 栈为空时,出现一个')',可以将sum置0.
public class Solution {
public int longestValidParentheses(String s) {
if (s == null) {
return 0;
}
Stack<Integer> stk = new Stack<Integer>();
int sum = 0;
int tmp = 0;
int max = 0;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c == '(') {
stk.push(i);
} else {
if (stk.isEmpty()) {
// 栈中没有'(',出现')', 则必须重置计算
sum = 0;
continue;
}
// count the temporary lenght:
// like: (()()()
// tmp = 2.
tmp = i - stk.pop() + 1;
if (stk.isEmpty()) {
// 有一套完整的括号集,可以加到前面的一整套括号集上
// () (()())
// 1 2 第二套括号集可以加过来
sum += tmp;
max = Math.max(sum, max);
} else {
// 也可能是一个未完成的括号集,比如:
// () (()() 在这里 ()() 是一个未完成的括号集,可以独立出来计算,作为
// 阶段性的结果
tmp = i - stk.peek();
max = Math.max(tmp, max);
}
}
}
return max;
}
}
2015.1.3 redo:
public class Solution {
public int longestValidParentheses(String s) {
if (s == null) {
return 0;
}
int len = s.length();
Stack<Integer> stk = new Stack<Integer>();
int sum = 0;
int max = 0;
for (int i = 0; i < len; i++) {
char c = s.charAt(i);
if (c == '(') {
stk.push(i);
} else {
if (stk.isEmpty()) {
// The sequence is cut off.
sum = 0;
} else {
int tmp = i - stk.pop() + 1;
if (stk.isEmpty()) {
sum += tmp;
max = Math.max(max, sum);
} else {
max = Math.max(max, i - stk.peek());
}
}
}
}
return max;
}
}
GIT HUB 代码:
https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/string/LongestValidParentheses.java
LeetCode: 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 ...
- [Leetcode] longest valid parentheses 最长的有效括号
Given a string containing just the characters'('and')', find the length of the longest valid (well-f ...
- [LeetCode] Longest Valid Parentheses
第一种方法,用栈实现,最容易想到,也比较容易实现,每次碰到‘)’时update max_len,由于要保存之前的‘(’的index,所以space complexity 是O(n) // 使用栈,时间 ...
- [LeetCode] Longest Valid Parentheses 动态规划
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- LeetCode: Valid Parentheses 解题报告
Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', det ...
- [LeetCode] Longest Valid Parentheses -- 挂动态规划羊头卖stack的狗肉
(Version 1.3) 这题在LeetCode上的标签比较有欺骗性,虽然标签写着有DP,但是实际上根本不需要使用动态规划,相反的,使用动态规划反而会在LeetCode OJ上面超时.这题正确的做法 ...
- leetcode: Longest Valid Parentheses分析和实现
题目大意:给出一个只包含字符'('和')'的字符串S,求最长有效括号序列的长度. 很有趣的题目,有助于我们对这种人类自身制定的规则的深入理解,可能我们大多数人都从没有真正理解过怎样一个括号序列是有效的 ...
- LeetCode: Longest Common Prefix 解题报告
Longest Common Prefix Write a function to find the longest common prefix string amongst an array of ...
随机推荐
- oracle 批量更新之update case when then
oracle 批量更新之update case when then CreationTime--2018年8月7日15点51分 Author:Marydon 1.情景描述 根据表中同一字段不同情况 ...
- 科学计算法帮助类MathUtils
import java.math.BigDecimal; import java.math.MathContext; import java.math.RoundingMode; /** * 科学计算 ...
- delattr
>>> class MyData(): def __init__(self,name,phone): self.name=name self.phone=phone def upda ...
- 蚂蚁金服互联网IT运维体系实践
摘要: 本文来自蚂蚁金服首席技术架构师,基础技术部负责人胡喜.从2010年支撑双十一最高交易峰值2万笔/分钟到2015年双十一的8.59万笔/秒,蚂蚁金服的技术架构和运维体系一直都在不断摸索和实践.本 ...
- spring注解 di 和 ioc 注解
注解: 1.注解就是为了说明java中的某一个部分的作用(Type) 2.注解都可以用于哪个部门是@Target注解起的作用 3.注解可以标注在ElementType枚举类所指定的位置上 4. @Do ...
- Hibernate 、 Axis2发布
1. you just compile your web-service into aar file (not include bean files), copy the aar into axis2 ...
- ubuntu(14.04) remote access(远程连接数据库)
1.修改mysql的配置文件. /etc/mysql/my.cnf 把 bind-address 的那行代码注释掉,保存退出,重启mysql
- 【ASP.NET Web API教程】2.4 创建Web API的帮助页面[转]
注:本文是[ASP.NET Web API系列教程]的一部分,如果您是第一次看本博客文章,请先看前面的内容. 2.4 Creating a Help Page for a Web API2.4 创建W ...
- Python学习笔记020——数据库基本操作
本数据库的操作是Linux虚拟机平台下进行的 1 启动和链接MySQL服务 1.1 服务端 (1)查看服务状态 sudo /etc/init.d/mysql stauts (2)启动服务端 sudo ...
- OAF_OAF控件系列1 - Region Type汇总(概念)
2014-06-22 Created By BaoXinjian