Question

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 -- Stack

Parenthese类的题目,第一反应是用栈。遇到'('入栈,遇到')'出栈。这题关键在于怎样知道当前的valid parenthese length。

我们用一个变量start来表示当前的第一个'(' 。

(  (  (  )  )  )  )  )  (

start                                                             start

当栈为空时,进来的第一个左括号的位置为当前的start值。

出栈操作后:

1. 如果栈为空,说明从start开始的括号都匹配了。因此长度为 current - start + 1

2. 如果栈不为空,我们知道弹出栈的一定是都已经匹配了的。因此长度为 current - stack.peek()

因为只扫描了一遍原数组,所以时间复杂度是O(n),空间复杂度是O(n)

 public class Solution {
public int longestValidParentheses(String s) {
if (s == null || s.length() < 1) {
return 0;
}
int length = s.length();
Deque<Integer> stack = new LinkedList<Integer>();
int start = 0;
int result = 0;
for (int i = 0; i < length; i++) {
char current = s.charAt(i);
if (current == '(') {
stack.push(i);
} else {
if (stack.isEmpty()) {
start = i + 1;
} else {
stack.pop();
result = stack.isEmpty() ? Math.max(result, i - start + 1) : Math.max(result, i - stack.peek());
}
}
}
return result;
}
}

Solution 2 -- DP

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. Longest Valid Parentheses

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

  4. leetcode 32. Longest Valid Parentheses

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

  5. 【leetcode】Longest Valid Parentheses

    Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...

  6. 【leetcode】 Longest Valid Parentheses (hard)★

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

  7. Longest Valid Parentheses 每每一看到自己的这段没通过的辛酸代码

    Longest Valid Parentheses My Submissions Question Solution  Total Accepted: 47520 Total Submissions: ...

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

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

  9. Java for LeetCode 032 Longest Valid Parentheses

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

随机推荐

  1. hdu 5012 Dice

    Problem Description There are 2 special dices on the table. On each face of the dice, a distinct num ...

  2. Python 中文报错 SyntaxError: Non-ASCII character解决办法

    只需要在最顶部的位置加上 #-*- coding:utf-8 -*- 就行了. 如果还是没有好,打开设置并搜索file encoding并作如图修改便好.

  3. GDB实用的调试工具

    GDB它是GNU Debuger缩写,它是GNU发表了unix通过应用程序调试工具. 它被广泛应用于在各种各种生产内部应用.GDB和所有的调试工具经常使用相同的,其主要特点是:监视变量的值.设置断点及 ...

  4. JavaScripts学习日记——BOM

    IE 3.0 和 Netscape Navigator 3.0 提供了一种特性 - BOM(浏览器对象模型),可以对浏览器窗口进行访问和操作.使用 BOM,开发者可以移动窗口.改变状态栏中的文本以及执 ...

  5. js调用百度地图搜索功能

    引用百度jsApi <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&a ...

  6. Web Api 跨域解决方案

    一.跨域问题的由来 同源策略:出于安全考虑,浏览器会限制脚本中发起的跨站请求,浏览器要求JavaScript或Cookie只能访问同域下的内容. 正是由于这个原因,我们不同项目之间的调用就会被浏览器阻 ...

  7. C#语法糖: 扩展方法(常用)

    今天继续分享C#4.0语法糖的扩展方法,这个方法也是我本人比较喜欢的方法.大家先想想比如我们以前写的原始类型不能满足现在的需求,而需要在该类型中添加新的方法来实现时大家会怎么做.我先说一下我没有学习到 ...

  8. PowerDesigner Mysql 主键自增、初始值、字符集

    自增 在你所要设为自增型的键上(比如你的id)双击,弹出一个Column Properties对话框,右下角有一个Identify的选择框,选中它OK,就可以了. 再去查看Preview,就能看到AU ...

  9. hdu4296 贪心

    E - 贪心 Crawling in process... Crawling failed Time Limit:2000MS     Memory Limit:32768KB     64bit I ...

  10. hdu Red and Black

    算法:深搜 题意:就是让你找到一共可以移动多少次,每次只能移到黑色格子上, Problem Description There is a rectangular room, covered with ...