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 ...
随机推荐
- MySQL优化之——权限管理
转载请注明出处:http://blog.csdn.net/l1028386804/article/details/46763767 在mysql数据库中,有mysql_install_db脚本初始化权 ...
- java stream collector
Java Stream API进阶篇 本文github地址 上一节介绍了部分Stream常见接口方法,理解起来并不困难,但Stream的用法不止于此,本节我们将仍然以Stream为例,介绍流的规约操作 ...
- Web应用的演变以及网络应用程序的架构的优缺点
(1)主机/终端 特点: 主机负责所有的计算(处理业务), 终端只负责输入输出(不做任何计算). 优点: 可靠,安全,i/o能力强. 缺点: 昂贵,扩展困 ...
- 【微信小程序】详解wx:if elif else的用法(搭配view、block)
1.搭配view <view wx:if="{{boolean==true}}"> <view class="bg_black">< ...
- awk打印倒数第2列
cat 1-iplist.txt | awk '{ print $(NF-2) }'|wc 实际示例: 打印nginx日志中 变量request_time超过3秒的日志信息 [root@datalin ...
- IT常用设备及服务器安全公告或漏洞地址pa安全预警微软安全公告 HP 安全公告AIX 安全公告REDHAT安全公告ORACLE 安全公告F5 安全公告 Cisco 安全公告Juniper 安全公告 VMware 安全公告IOS 安全公告
IT常用设备及服务器安全公告或漏洞地址: 微软安全公告 https://technet.microsoft.com/en-us/library/security/MS14-085 HP 安全公告 ht ...
- Android——Intent动作汇总(转)
String ADD_SHORTCUT_ACTION 动作:在系统中添加一个快捷方式.. "android.intent.action.ADD_SHORTCUT" String A ...
- 解决spring el表达式不起作用
el表达式不起作用,如下图所示 现象: 在显示页面中加入: <%@ page isELIgnored="false" %>就OK了 参考:http://bbs.csdn ...
- GO1.6语言学习笔记2-安装配置及代码组织
一.关于GO开发环境的安装和配置 在linux环境中安装编译好的go安装包,参考官方指南的步骤一步步走下来就可以了.需要注意的是以下几个环境变量的配置: GOROOT - ...
- 关于centos7中使用rpm方式安装mysql5.7版本后无法使用root登录的问题
最近在centos7中通过rpm方式安装了最新版本的mysql-server 5.7 (mysql57-community-release-el7-7.noarch.rpm) ,发现安装成功后无法使用 ...