题目描述: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. Java学习的第五十二天

    1.例9.4对象数组的使用方法 public class Cjava { public static void main(String[]args) { Box b[] = {new Box(10,1 ...

  2. Django+Celery+xadmin实现异步任务和定时任务

    Django+Celery+xadmin实现异步任务和定时任务 关注公众号"轻松学编程"了解更多. 一.celery介绍 1.简介 [官网]http://www.celerypro ...

  3. python机器学习的开发流程

    标准机器学习的开发编程流程 关注公众号"轻松学编程"了解更多. 一.流程 标准机器学习的开发编程流程: 1.获取数据(爬虫.数据加载.业务部门获取) 2.数据建模(摘选样本数据(特 ...

  4. springcloud-zinpin的安装与使用

    springcloud-zipkin的安装与使用 1.什么是zipkin 一个分布式系统的调用跟踪监控系统,把每次微服务调用都埋上点,打印固定格式的日志,然后收集到zipkin中,然后zipkin做数 ...

  5. 压缩法备份Linux系统文件

    在使用Ubuntu之前,相信很多人都有过使用Windows系统的经历.如果你备份过Windows系统,那么你一定记忆犹新:首先需要找到一个备份工 具(通常都是私有软件),然后重启电脑进入备份工具提供的 ...

  6. ASP.NET Core 中基于工厂的中间件激活

    IMiddlewareFactory/IMiddleware 是中间件激活的扩展点. UseMiddleware 扩展方法检查中间件的已注册类型是否实现 IMiddleware. 如果是,则使用在容器 ...

  7. SpringBoot进阶教程(六十四)注解大全

    在Spring1.x时代,还没出现注解,需要大量xml配置文件并在内部编写大量bean标签.Java5推出新特性annotation,为spring的更新奠定了基础.从Spring 2.X开始spri ...

  8. SpringBoot中BeanValidation数据校验与优雅处理详解

    目录 本篇要点 后端参数校验的必要性 不使用Validator的参数处理逻辑 Validator框架提供的便利 SpringBoot自动配置ValidationAutoConfiguration Va ...

  9. redis乐观锁

    乐观锁(又名乐观并发控制,Optimistic Concurrency Control,缩写"OCC"),是一种并发控制的方法.它假设多用户并发的事务在处理时不会彼此互相影响,各事 ...

  10. arm64大服务器安装ubuntu18看不到安装界面

    前言 最近在使用arm的大服务器需要用到ubuntu相关的一些东西,在操作系统安装过程中遇到了一些问题 记录 华为鲲鹏服务器 这个默认安装centos的都很顺利,安装ubuntu18最新的,impi就 ...