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 ...
随机推荐
- str.format格式化用法(通过{}来替代%)
# -*- coding: utf-8 -*- #python 27 #xiaodeng #str.format格式化用法(通过{}来替代%) ''' >>> help(format ...
- 【LeetCode】【Python题解】Reverse Integer
Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to ...
- 【laravel5.4】git上clone项目到本地,配置和运行 项目报错:../vendor/aotuload.php不存在
1.一般我们直接使用git clone 将git的项目克隆下来,在本地git库和云上git库建立关联关系 2.vendor[扩展]文件夹是不会上传的,那么下载下来直接运行项目,会报错: D:phpSt ...
- HDUOJ----4501小明系列故事——买年货(三维背包)
小明系列故事——买年货 Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total ...
- Python 的 if __name__ == '__main__'
Python 文件 最后部分会有: if __name__ == '__main__': TestRLSO()……………… 1)首先,这是一个判断语句. 表示执行的是此代码所在的文件.如果这个文件是作 ...
- Linux基础之常用命令篇
一.命令的基本格式 [root@localhost~] root为用户名 -表示当前所在位置 localhost主机名 ‘#’超级用户 '$" 普通用户 命令的基本格式: 命令 [选项] [ ...
- 阅读《Android 从入门到精通》(17)——进度条
进度条(ProgressBar) java.lang.Object; android.view.View; android.widget.ProgressBar; ProgressBar 类方法 Pr ...
- 怎样让VMware上的虚拟机ping通外网(图解教程)
近期在实习项目中遇到一个问题. 因測试须要,本人在win7上安装VMWare后在启动两台ubuntuserver.两台主机的网络配置所有採用NAT方式实现连接. 之后一路畅通.主机ping通虚拟机和外 ...
- Mysql 数据备份与恢复,用户创建,授权
Mysql 数据备份与恢复,用户创建,授权 1. Mysqldump >outfile.sql 2. Mysql –uxxx –pxxx < backfile.sql 3. Create ...
- Shell基础知识和编程规范
一,Shell环境查看 1.1 查看系统Shell支持情况 [root@linux-node1 ~]# cat /etc/shells /bin/sh /bin/bash /sbin/nologin ...