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.

int longestValidParentheses(string s) {
int maxLen = ;
int lastError = -;
vector<int> stack;
for(int i=; i<s.size(); i++)
{
if (s[i] == '(')
stack.push_back(i);
else if (s[i] == ')')
{
if (stack.size()> )
{
stack.pop_back();
int len;
if (stack.size()==)
len = i - lastError;
else
len = i - stack.back();
if (len > maxLen)
maxLen = len;
}
else
lastError = i;
}
}
return maxLen;
}

32. Longest Valid Parentheses *HARD*的更多相关文章

  1. [Leetcode][Python]32: Longest Valid Parentheses

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 32: Longest Valid Parentheseshttps://oj ...

  2. leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、

    20. Valid Parentheses 错误解法: "[])"就会报错,没考虑到出现')'.']'.'}'时,stack为空的情况,这种情况也无法匹配 class Soluti ...

  3. 刷题32. Longest Valid Parentheses

    一.题目说明 题目是32. Longest Valid Parentheses,求最大匹配的括号长度.题目的难度是Hard 二.我的做题方法 简单理解了一下,用栈就可以实现.实际上是我考虑简单了,经过 ...

  4. [LeetCode] 32. Longest Valid Parentheses 最长有效括号

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

  5. leetcode 32. Longest Valid Parentheses

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

  6. 32. Longest Valid Parentheses

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

  7. Java [leetcode 32]Longest Valid Parentheses

    题目描述: Given a string containing just the characters '(' and ')', find the length of the longest vali ...

  8. leetcode problem 32 -- Longest Valid Parentheses

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

  9. 【Python】32. Longest Valid Parentheses

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

  10. 【一天一道LeetCode】#32. Longest Valid Parentheses

    一天一道LeetCode系列 (一)题目 Given a string containing just the characters '(' and ')', find the length of t ...

随机推荐

  1. python中的property

    提示:这篇博文参考了两个博客,第一篇博文地址为:https://www.cnblogs.com/Lambda721/p/6132206.html,另一篇博文地址如下:关于python的property ...

  2. Android实践项目汇报(三)

    Google天气客户端 本周学习计划 调试代码使之成功运行并实现天气预报功能. 实际完成情况 由于google取消api接口服务,天气源的传输.所以我换了一个使用 haoserver API接口的程序 ...

  3. Linux内存管理--虚拟地址、逻辑地址、线性地址和物理地址的区别(二)【转】

    本文转载自:http://blog.csdn.net/yusiguyuan/article/details/9668363 这篇文章中介绍了四个名词的概念,下面针对四个地址的转换进行分析 CPU将一个 ...

  4. win10 系统变量迁移

    经常要重装电脑,自己的很多配置都要重新手动配置,其中就包括系统变量 系统变量在注册表中存在 win+R regedit HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\ ...

  5. 格子中输出|2015年蓝桥杯B组题解析第四题-fishers

    StringInGrid函数会在一个指定大小的格子中打印指定的字符串. 要求字符串在水平.垂直两个方向上都居中. 如果字符串太长,就截断. 如果不能恰好居中,可以稍稍偏左或者偏上一点. 下面的程序实现 ...

  6. Type.Missing和System.Reflection.Missing.Value

    Type.Missing https://msdn.microsoft.com/en-us/library/system.type.missing(v=vs.110).aspx Missing.Val ...

  7. C# Byte[] 数组操作

    byte[] Strbyte = Encoding.GetEncoding("big5").GetBytes(str);            if (Strbyte.Length ...

  8. 解决 mininet gave up after 3 retries 问题

    解决 mininet gave up after 3 retries 问题 在通过mn启动mininet的时候遇到了如下问题: *** Creating network *** Adding cont ...

  9. python闭包closure

    在讨论闭包之前,先总结一下python的命名空间namespace,一般的语言都是通过namespace来识别名字标识,无论是变量,对象,函数等等.python划分3个名字空间层次,local:局部, ...

  10. 使用rviz 查看远程主机

    一.安装好ros环境 https://www.cnblogs.com/sea-stream/p/9809590.html 二.配置参数 vim ~/.bashrc #输入内容 export ROS_H ...