第一种方法,用栈实现,最容易想到,也比较容易实现,每次碰到‘)’时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. Java发送邮件代码

    MailSenderInfo.java package com.nihaorz.mail.util; import java.util.Properties; public class MailSen ...

  2. MVC 基架不支持 Entity Framework 6 或更高版本

    MVC 基架不支持 Entity Framework 6 或更高版本.有关详细信息,请访问 http://go.microsoft.com/fwlink/?LinkId=276833. PS:新做一个 ...

  3. 【POJ 3321】Apple Tree

    有n个节点以1为根节点的树,给你树的边关系u-v,一开始每个节点都有一个苹果,接下来有两种操作,C x改变节点x的苹果状态,Q x查询x为根的树的所有苹果个数.   求出树的dfs序,st[i]保存i ...

  4. OO(Object Oriented)思想和PO(Procedure-Oriented)思想

    对象将需求用类一个个隔开,就象用储物箱把东西一个个封装起来一样,需求变了,分几种情况,最严重的是大变,那么每个储物箱都要打开改,这种方法就不见得有好处:但是这种情况发生概率比较小,大部分需求变化都是局 ...

  5. 【BZOJ-4455】小星星 容斥 + 树形DP

    4455: [Zjoi2016]小星星 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 204  Solved: 137[Submit][Status] ...

  6. ubuntu安装mysql--参考的网址

    Ubuntu服务器常用配置-mysql数据库的安装 - SegmentFault MySQL 社区-你身边最优秀的MySQL中文社区! MySQL咨询,MySQL培训,MySQL优化 - Powere ...

  7. 天气查询SDK

    简介: 这是一个用于查询天气的SDK,在很多时候,尤其是对接多而小功能公众账号的时候,天气查询比较使用,此SDK就是这样的用途,使用的是中国天气网的API,已经集成了网上最靠谱的方式来实现,包括里面的 ...

  8. Codeforces Round #389 Div.2 D. Santa Claus and a Palindrome

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  9. shutil模块

    shutil模块 提供了大量的文件的高级操作,特别针对文件拷贝和删除,主要功能为目录和文件操作以及压缩操作 常用方法 shutil.copyfile(src, dst) 复制文件内容(不包含元数据)从 ...

  10. FZU 2191 完美的数字

    题目链接: 传送门 完美的数字 Time Limit: 1000MS     Memory Limit: 65536K 题目描述 Bob是个很喜欢数字的孩子,现在他正在研究一个与数字相关的题目,我们知 ...