第一种方法,用栈实现,最容易想到,也比较容易实现,每次碰到‘)’时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. MyEclipse删除不再使用的工作空间记录

    找到安装目录下的configuration/.settings/org.eclipse.ui.ide.prefs文件,打开此文件,删除不再使用的工作空间信息,重启MyEclipse.然后在切换工作空间 ...

  2. poj1182 带权并查集

    食物链 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 60225   Accepted: 17656 Description ...

  3. 【LintCode】计算两个数的交集(二)

    问题分析: 用两个指针分别遍历即可. 问题求解: public class Solution { /** * @param nums1 an integer array * @param nums2 ...

  4. Maven项目加载JAR包

    登陆网站找jar包:http://mvnrepository.com/ 1.http://mvnrepository.com/artifact/net.sf.jxls/jxls-core/1.0.6 ...

  5. js-关于性能优化的一些学习总结

    性能优化的方法有: 1.减少HTTP请求:合并CSS/JS,使用CSS sprite等 2.压缩CSS/JS/图片 3.样式表放头部,JS放body底部:JS放在head中,将会等到js全部下载解析和 ...

  6. 【bzoj1922】 Sdoi2010—大陆争霸

    http://www.lydsy.com/JudgeOnline/problem.php?id=1922 (题目链接) 题意 一张无向图,每个节点被k个节点保护,想要走到一个节点当且仅当它不被保护.你 ...

  7. NOI2016模拟赛Zbox loves stack

    #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...

  8. iOS 关于本地持久化存储的探讨

    目前,用以本地化存储的方式有很多,常用的有以下: 1.临时缓存 先说说临时缓存,临时缓存一般相当于用来管理应用程序中全局需要常用的一些内容.比如当前用户的ID或者当前的定位信息等. 常用的方式就是写一 ...

  9. [Java 实现AES加密解密]

    今天同学请教我这个问题,被坑了次…… 实现的功能是2个Java类:一个读取源文件生成加密文件,另一个类读取加密文件来解密. 整个过程其实很简单,java有AES的工具包,设好秘钥,设好输入内容,就得到 ...

  10. java循环遍历map

    import java.util.HashMap; import java.util.Iterator; import java.util.Map; public class MapTest { pu ...