description:

Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring.

找符合规则的最长的括号

Note:

Example:

Example 1:

Input: "(()"
Output: 2
Explanation: The longest valid parentheses substring is "()"
Example 2: Input: ")()())"
Output: 4
Explanation: The longest valid parentheses substring is "()()"

answer:

class Solution {
public:
int longestValidParentheses(string s) {
int res = 0, start = 0;
stack<int> m;
for (int i = 0; i < s.size(); ++i) {
if (s[i] == '(') m.push(i);
else if (s[i] == ')') {
if (m.empty()) start = i + 1;
else {
m.pop();
res = m.empty() ? max(res, i - start + 1) : max(res, i - m.top());
//取出去之后同样有两种情况讨论
}
}
}
return res;
}
};

relative point get√:

hint :

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

  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. 【Python】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] 32. Longest Valid Parentheses 最长有效括号

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

  7. leetcode 32. Longest Valid Parentheses

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

  8. 32. Longest Valid Parentheses

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

  9. Java [leetcode 32]Longest Valid Parentheses

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

随机推荐

  1. 大作!webpack详细配置

    webpack学习之旅 好好学习 天天向上!遇到bug,不要慌! 文章目录 webpack学习之旅 大一统的模块化规范--ES6模块化 1.node.js中通过babel体验ES6模块化 2.ES6模 ...

  2. 在.NET 6中使用DateOnly和TimeOnly

    千呼万唤始出来 在.NET 6(preview 4)中引入了两个期待已久的类型,将作为核心库的一部分.DateOnly和TimeOnly允许开发人员表示DateTime的日期或时间部分.这两个类型为值 ...

  3. RabbitMQ(1)学习目标

    一:安装,专业术语,简单队列,工作队列,发布/订阅队列,路由队列,主题队列,RPC队列,事务,确认模式,SpringAMQP 二:什么是MQ? MQ就是消息队列,是一种进程间通信或同一进程的不同线程间 ...

  4. Redis 内存大小限制+键值淘汰策略配置

    限制最大内存 windows 的 maxmemory-policy 策略可能会少一些 # 指定 Redis 最大内存限制,Redis 在启动时会把数据加载到内存中,达到最大内存后,Redis 会先尝试 ...

  5. Waymo的激光雷达计划:进展如何?

    Waymo的激光雷达计划:进展如何? Waymo's Lidar Plan: How's It Working out? 许多自动驾驶汽车(AV)开发商一直在热烈追求激光雷达技术,这一技术之所以重要, ...

  6. Java日期时间API系列39-----中文语句中的时间语义识别(time NLP 输入一句话,能识别出话里的时间)原理分析

    NLP (Natural Language Processing) 是人工智能(AI)的一个子领域.自然语言是人类智慧的结晶,自然语言处理是人工智能中最为困难的问题之一(来自百度百科). 其中中文更是 ...

  7. Markdown个人常用语法

    Markdown实用格式 标题 # 标题一级 ## 标题二级 ### 标题三级 #### 标题四级 ##### 标题五级 ###### 标题六级 粗体.斜体和删除线 **加粗字体** *斜体字体* * ...

  8. 性能报告之HTML5 性能测试报告

    1. 引言 1.1. 编写目的 HTML5 作为当前"最火"的跨平台.跨终端(硬件)开发语言,越来越受到前端开发者 的重视,无论是 PC 端还是当前"火热"的移 ...

  9. 【Android漏洞复现】StrandHogg漏洞复现及原理分析_Android系统上的维京海盗

    文章作者MG1937 CNBLOG博客:ALDYS4 QQ:3496925334 0x00 StrandHogg漏洞详情 StrandHogg漏洞 CVE编号:暂无 [漏洞危害] 近日,Android ...

  10. 【工具解析】瑞士军刀bettercap2.X_解析_第二期_内网钓鱼(嗅探)工具编写

    /文章作者:Kali_MG1937 CNBLOG博客:ALDYS4 QQ:3496925334/ 第一期: https://www.cnblogs.com/aldys4/p/14877783.html ...