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. xml 解析之 JDOM解析

    JDOM 是一种使用 XML 的独特 Java 工具包,用于快速开发 XML 应用程序.JDOM 是一个开源项目,它基于树形结构,利用纯 Java 的技术对 XML 文档实现解析.生成.序列化及多种操 ...

  2. Python for循环 - Python零基础入门教程

    目录 一.for 循环语法 二.for 循环实战 三.重点总结 四.猜你喜欢 零基础 Python 学习路线推荐 : Python 学习目录 >> Python 基础入门 在 Python ...

  3. unity用json和服务器数据交互

    第一种类型:服务器json数据是个对象 /// <summary> /// 获取用户信息初始化信息 /// </summary> void InitUserMessage() ...

  4. Archlinux zsh终端美化 powerlevel10k配置

    环境 Arch Linux + KDE Plasma 安装zsh yay -S zsh 更改默认终端 chsh -s /bin/zsh 安装oh-my-zsh-git archlinuxcn源有打好的 ...

  5. IP子网如何划分?so easy!

    IP地址与子网掩码 1. IP地址划分 1.1 IP地址 1.2 由两部分组成 1.3 IP地址的分类 1.4 IP地址的规划原则 2.子网掩码划分 2.1 32个二进制位 2.2IP地址和子网掩码作 ...

  6. GO学习-(19) Go语言基础之网络编程

    Go语言基础之网络编程 现在我们几乎每天都在使用互联网,我们前面已经学习了如何编写Go语言程序,但是如何才能让我们的程序通过网络互相通信呢?本章我们就一起来学习下Go语言中的网络编程. 关于网络编程其 ...

  7. 在js中将map对象转换成json 和 js对cookie的操作

    在js中将map对象转换成json //msp转objectlet obj= Object.create(null); for (let[k,v] of map) { obj[k] = v; }//o ...

  8. 摄像头与毫米波雷达(Radar)融合

    摄像头与毫米波雷达(Radar)融合 Input: (1)图像视频分辨率(整型int) (2)图像视频格式 (RGB,YUV,MP4等) (3)毫米波雷达点云信息(点云坐标位置x,y,浮点型float ...

  9. TensorRT Analysis Report分析报告

    TensorRT Analysis Report 一.介绍 TensorRT是一个高性能的深度学习推理(Inference)优化器,可以为深度学习应用提供低延迟.高吞吐率的部署推理.TensorRT可 ...

  10. awr快照保留时间修改

    ==============  awr快照保留时间修改 ============= 1.查询当前awr报告保留时间 col SNAP_INTERVAL for a20col RETENTION for ...