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

 // 使用栈,时间复杂度 O(n),空间复杂度 O(n)
class Solution {
public:
int longestValidParentheses(string s) {
int max_len = , last = -;
stack<int> lefts;
for (int i = ; i < s.size(); ++i) {
if (s[i] =='(') {
lefts.push(i);
} else {
if (lefts.empty()) {
// update last
last = i;
} else {
// find a matching pair
lefts.pop();
if (lefts.empty()) {
max_len = max(max_len, i-last);
} else {
max_len = max(max_len, i-lefts.top()); }
}
}
}
return max_len;
}
};

第二种方方法,搞一个计数器,每次遇到'('加一,遇到‘)’ 减少一,所以只有在计数器==0的时候更新 max_len。为了实现功能,必须两侧扫描,从而在计数器==0的时候更新max_len.

例如( ( () () 由于左括号多,因此从左侧扫描无法达到计数器==0,所以,max_len还是0.但当从右侧扫描是,右括号较少,会达到0,从而得到真实的max_len.保存start的思想和栈方法一致。

 // LeetCode, Longest Valid Parenthese
// 两遍扫描,时间复杂度 O(n),空间复杂度 O(1)
// @author 曹鹏
class Solution {
public:
int longestValidParentheses(string s)
{
int answer = , depth = , start = -;
for (int i = ; i < s.size(); ++i) {
if (s[i] == '(') {
++depth;
} else {
--depth;
if (depth < ) {
start = i;
depth = ;
} else if (depth == ) {
answer = max(answer, i - start);
}
}
}
depth = ;
start = s.size();
for (int i = s.size() - ; i >= ; --i) {
if (s[i] == ')') {
++depth;
} else {
--depth;
if (depth < ) {
start = i;
depth = ;
} else if (depth == ) {
answer = max(answer, start - i);
}
}
}
return answer;
}
};

[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 动态规划

    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 -- 挂动态规划羊头卖stack的狗肉

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

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

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

  8. leetcode Longest Valid Parentheses python

    class Solution(object): def longestValidParentheses(self, s): """ :type s: str :rtype ...

  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. zoj1492 最大团

    Maximum Clique Time Limit: 10 Seconds      Memory Limit: 32768 KB Given a graph G(V, E), a clique is ...

  2. html-div自动撑大

    下面提供几种解决方案,以修复该问题. 1.给父容器使用display属性 div#container { display: table; /* 建议使用 */ /*或者 display: table- ...

  3. 虚拟机NAT模式无法上网问题的解决办法

    在使用CentOS虚拟机时,出现了无法上网的情况,使用主机ping虚机地址可以ping通,而虚机ping不通主机,同时虚机也无法ping通其他的网址或ip,显示内容为Network is unreac ...

  4. url 字符串中的参数信息

    /// <summary>        /// 分析 url 字符串中的参数信息        /// </summary>        /// <param nam ...

  5. C++ 异常机制分析

    C++异常机制概述 异常处理是C++的一项语言机制,用于在程序中处理异常事件.异常事件在C++中表示为异常对象.异常事件发生时,程序使用throw关键字抛出异常表达式,抛出点称为异常出现点,由操作系统 ...

  6. JVM学习笔记:虚拟机的类加载机制

    JVM类加载机制分两部分来总结: (1)类加载过程 (2)类加载器 一.JVM类加载过程 类的加载过程:加载 →连接(验证 → 准备 → 解析)→ 初始化. 类的生命周期:加载 →连接(验证 → 准备 ...

  7. Leetcode 416. Partition Equal Subset Sum

    Given a non-empty array containing only positive integers, find if the array can be partitioned into ...

  8. HDU2203 亲和串

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000作者博客:http://www.cnblogs.com/ljh2000-jump/转 ...

  9. poj3744 Scout YYF I

    题意:n个地雷(n<=10)在长度10^8的坐标轴上,yyf从横坐标为1的点开始,每一步有p的概率向右跳一格,(1-p)的概率向右跳两格(不会踩到中间一格),如果踩到地雷他就会死.问活下来的概率 ...

  10. struts2 CVE-2012-0392 S2-008 Strict DMI does not work correctly allows remote command execution and arbitrary file overwrite

    catalog . Description . Effected Scope . Exploit Analysis . Principle Of Vulnerability . Patch Fix 1 ...