题目描述:Longest Valid Parentheses

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.

代码如下:

class Solution {
public:
int longestValidParentheses(string s) { int max_len = 0, last = -1; //last是上一次的')'的位置
stack<int> lefts; for(int i = 0; i < s.size(); i++){ //s[i]为'(',则将i入栈
if(s[i] == '(')
lefts.push(i); //s[i]为')'
else{
if(lefts.empty())
last = i;
else{
lefts.pop(); //若栈为空,则
if(lefts.empty())
max_len = max(max_len, i - last);
else
max_len = max(max_len, i - lefts.top());
}
}
} return max_len; }
};

LeetCode 032 Longest Valid Parentheses的更多相关文章

  1. [LeetCode] 032. Longest Valid Parentheses (Hard) (C++)

    指数:[LeetCode] Leetcode 指标解释 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 032. Lon ...

  2. Java for LeetCode 032 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 v ...

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

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

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

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

  8. Java [leetcode 32]Longest Valid Parentheses

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

  9. [leetcode]32. Longest Valid Parentheses最长合法括号子串

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

随机推荐

  1. 关于python递归函数,这样写就对了

    大家好我是致力于让每个人都能够轻松学会编程的小梁,在这条路上任重道远,关注我,每天让您获取来自编程的乐趣. 关注公众号"轻松学编程".了解更多. 今天就给大家分享一下关于使用递归函 ...

  2. C#4语法新特性

    C#4,.NET Framework 4.0, Visual Studio 2010  C#4.0新引进的语法基于.Net Framework 4.0.主要引进的语法:动态类型,命名参数.可选参数,优 ...

  3. 转载-Eclipse无法打开Eclipse MarketPlace的解决办法

    问题描述: Eclipse点击 help-->Eclipse MarketPlace 后无任何反应,无报错,打不开 解决方法: 重新安装一下 epp MarketPlace help--> ...

  4. JVM学习(五) -执行子系统

    虚拟机和物理机的区别.两种都有代码执行能力.物理机的执行引擎是建立在处理器.硬件.指令集和操作系统上.而虚拟机的执行引擎是有自己实现的.因此可以自行的制定指令集和执行引擎的结构关系. 个人理解:分为三 ...

  5. 《Web接口开发与自动化测试》学习笔记(一)

    一.Django的入门 学习思路:先安装Django,然后在建立一个项目,接着运行这个项目,最后修改一下这个项目的数据,学习一下Django的原理之类的. 1.安装Django $pip instal ...

  6. iNeuOS工业互联平台,WEB组态(iNeuView)增加工程视图导入、导出功能,及优化和修复,发布:v3.2.1版本

    目       录 1.      概述... 2 2.      平台演示... 2 3.      导出组态工程文件... 2 4.      导入组态工程文件... 3  1.   概述 iNe ...

  7. ABP框架中一对多,多对多关系的处理以及功能界面的处理(1)

    在我们开发业务的时候,一般数据库表都有相关的关系,除了单独表外,一般还包括一对多.多对多等常见的关系,在实际开发过程中,需要结合系统框架做对应的处理,本篇随笔介绍基于ABP框架对EF实体.DTO关系的 ...

  8. git clone 出现"error: RPC failed; curl 56 GnuTLS recv error (-9): A TLS packet with unexpected length was received."

    1. 最近用git pull几个大项目,总是报如下错误: error: RPC failed; curl 56 GnuTLS recv error (-9): A TLS packet with un ...

  9. 调试HotSpot源代码(配视频)

    本文将详细介绍在Ubuntu16.04 LTS上对OpenJDK8进行编译,为了方便大家快速搭建起OpenJDK8的调试开发环境,我还录制了对应的视频放到了B站上,大家可以参考. 视频地址:https ...

  10. python之 栈与队列

    忍不住想报一句粗口"卧槽"这尼玛python的数据结构也太特么方便了吧 想到当初学c语言的数据结构的时候,真的是一笔一划都要自己写出来,这python尼玛直接一个模块就ok 真的是 ...