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 解题报告的更多相关文章

  1. [LeetCode] Longest Valid Parentheses 解题思路

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  2. [LeetCode] Longest Valid Parentheses 最长有效括号

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  3. [Leetcode] longest valid parentheses 最长的有效括号

    Given a string containing just the characters'('and')', find the length of the longest valid (well-f ...

  4. [LeetCode] Longest Valid Parentheses

    第一种方法,用栈实现,最容易想到,也比较容易实现,每次碰到‘)’时update max_len,由于要保存之前的‘(’的index,所以space complexity 是O(n) // 使用栈,时间 ...

  5. [LeetCode] Longest Valid Parentheses 动态规划

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  6. LeetCode: Valid Parentheses 解题报告

    Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', det ...

  7. [LeetCode] Longest Valid Parentheses -- 挂动态规划羊头卖stack的狗肉

    (Version 1.3) 这题在LeetCode上的标签比较有欺骗性,虽然标签写着有DP,但是实际上根本不需要使用动态规划,相反的,使用动态规划反而会在LeetCode OJ上面超时.这题正确的做法 ...

  8. leetcode: Longest Valid Parentheses分析和实现

    题目大意:给出一个只包含字符'('和')'的字符串S,求最长有效括号序列的长度. 很有趣的题目,有助于我们对这种人类自身制定的规则的深入理解,可能我们大多数人都从没有真正理解过怎样一个括号序列是有效的 ...

  9. LeetCode: Longest Common Prefix 解题报告

    Longest Common Prefix Write a function to find the longest common prefix string amongst an array of ...

随机推荐

  1. 混合用法模式 __name__和__main__

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #混合用法模式 __name__和__main__ #可把文件作为模块导入,并以独立式程序的形式运行,每个模块 ...

  2. 金山PDF

    金山是个很不错的软件公司,金山出PDF,纯粹是完善生态圈!毕竟没FoxitReader专业对PDF的处理上! 官网:芝麻开门 下载:http://wdl1.cache.wps.cn/wps/downl ...

  3. HDUOJ------1058 Humble Numbers

    Humble Numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  4. [转]MegCup2015初赛题

    原文链接 门票题:数独有多少种对解线上没有1的填法? 这道"门票题"虽说只是"热身",但还是有一定难度的.共有245名选手通过各种方法拿到了门票.下面,我们就为 ...

  5. tomcat使用方法大全

    一.安装tomcat之后 tomcat压缩包解压之后,进入webapps目录,可以看到如下webapp: docs文档:这是一个静态页面集,不用启动tomcat也可以阅读 examples样例 hos ...

  6. RabbitMQ消息队列(三):任务分发机制[转]

    在上篇文章中,我们解决了从发送端(Producer)向接收端(Consumer)发送“Hello World”的问题.在实际的应用场景中,这是远远不够的.从本篇文章开始,我们将结合更加实际的应用场景来 ...

  7. 在程序开发中怎样写SQL语句可以提高数据库的性能

    以下内容是公司dba总结. 1. 首先要搞明白什么叫执行计划?   执行计划是数据库根据SQL语句和相关表的统计信息作出的一个查询方案,这个方案是由查询优化器自动分析产生的,比如一条SQL语句如果用来 ...

  8. js 数据结构-栈与队列

    /*[客栈的盘子/月井里的货物,后进先出]栈顶:最先入口/出口的位置栈底:最慢最晚出栈的位置*/ function Stack() { var item = []; //推(将货物推入月井) this ...

  9. Vim进阶技术:搜索和替换

    行内搜索 行内搜索,也就是在当前行内进行搜索和移动,通常都与编辑命令一起使用. fx -- 移动到下一个字符x的位置,光标停留在x字符上面 tx -- 移动到下一个字符x的位置,光标停留在x前一个字符 ...

  10. php里面的注解(通过反射获取注解)

    /** * Created by PhpStorm. * User: Administrator * Date: 2018\10\12 0012 * Time: 14:30 */ /** * clas ...