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. springmvc自己定义拦截器

    Spring MVC也能够使用拦截器对请求进行拦截处理,用户能够自己定义拦截器来实现特定的功能,自己定义的拦截器必须实现HandlerInterceptor接口. 直接看下样例: package co ...

  2. 怎样让CodeBlocks支持C99

    转载请注明出处,否则将追究法律责任http://blog.csdn.net/xingjiarong/article/details/47080303 CodeBlocks是一个写C/C++的比較好的编 ...

  3. iOS-启动动态页跳过设计思路

    概述 根据UIBezierPath和CAShapeLayer自定义倒计时进度条,适用于app启动的时候设置一个倒计时关闭启动页面.可以设置进度条颜色,填充颜色,进度条宽度以及点击事件等. 详细 代码下 ...

  4. Linux各主要发行版的包管理命令对照

    Linux各主要发行版的包管理命令对照 Debian使用的是apt和dpkg,Gentoo则用的是emerge,Redhat的yum.Suse的zypper.Arch的pacman.Slackware ...

  5. 【转载】Delphi下实现鼠标自动点击器

    本文最早于2009年6月1日在编程论坛(programbbs.com)上发表,页面地址:http://programbbs.com/bbs/view12-20849-1.htm . 众所周知,当鼠标指 ...

  6. go语言学习 ---iota

    iota iota,特殊常量,可以认为是一个可以被编译器修改的常量. iota 在 const关键字出现时将被重置为 0(const 内部的第一行之前),const 中每新增一行常量声明将使 iota ...

  7. malloc的内存分配原理

    0 堆内存的在计算机内存中的形式 根据<The C Programming language>推测得到堆内存,图中的Heap区域即为堆内存块(Heap区域的数目不代表计算机堆内存的真实数目 ...

  8. Externalizable的使用方法

    package com.itbuluoge.object; import java.io.Externalizable; import java.io.FileInputStream; import ...

  9. Python 字典 copy()方法

    描述 Python 字典 copy() 方法返回一个字典的浅拷贝(父不变,子变). 语法 copy() 方法语法: D.copy() 参数 无. 返回值 返回一个字典的浅拷贝(父不变,子变). 实例 ...

  10. UVa 10298 - Power Strings

    题目:求一个串的最大的循环次数. 分析:dp.KMP,字符串.这里利用KMP算法. KMP的next函数是跳跃到近期的串的递归结构位置(串元素取值0 ~ len-1): 由KMP过程可知: 假设存在循 ...