32. Longest Valid Parentheses **堆栈
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 **堆栈的更多相关文章
- [Leetcode][Python]32: Longest Valid Parentheses
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 32: Longest Valid Parentheseshttps://oj ...
- leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、
20. Valid Parentheses 错误解法: "[])"就会报错,没考虑到出现')'.']'.'}'时,stack为空的情况,这种情况也无法匹配 class Soluti ...
- 刷题32. Longest Valid Parentheses
一.题目说明 题目是32. Longest Valid Parentheses,求最大匹配的括号长度.题目的难度是Hard 二.我的做题方法 简单理解了一下,用栈就可以实现.实际上是我考虑简单了,经过 ...
- 【Python】32. Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- 【LeetCode】32. Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- [LeetCode] 32. Longest Valid Parentheses 最长有效括号
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- leetcode 32. Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- 32. Longest Valid Parentheses
题目: Given a string containing just the characters '(' and ')', find the length of the longest valid ...
- Java [leetcode 32]Longest Valid Parentheses
题目描述: Given a string containing just the characters '(' and ')', find the length of the longest vali ...
随机推荐
- 10.8 ss:查看网络状态
ss命令 是类似并将取代netstat的工具,它能用来查看网络状态信息,包括TCP.UDP连接.端口等.它的优点是能够显示更多更详细的有关网络连接状态的信息,而且比netstat更快速更高效. ...
- python 中的变量内存以及关于is ==、 堆栈、
在工作学习中会碰到一些python中变量与内存层面的问题理解,虽然是在不断的解决,但是并没有做过这方面的总结. 变量:用来标识(identify)一块内存区域.为了方便表示内存,我们操作变量实质上是在 ...
- IDEA 配置 Tomcat(详细)(Day_12)
如果这世界上真有奇迹,那只是努力的另一个名字.生命中最难的阶段,不是没有人懂你,而是你不懂你自己. 运行环境 AND 版本 JDK8 + IntelliJ IDEA 2018.3 + Tomca ...
- Kubernetes集群搭建 ver1.20.5
目录 部署方式 1. 基础环境准备 1.1 基础初始化 1.2 安装docker 2. 部署harbor及haproxy高可用反向代理 2.1 镜像加速配置 2.2 高可用master可配置 3. 初 ...
- WEB安全防护相关响应头(下)
前篇"WEB安全防护相关响应头(上)"中,我们分享了 X-Frame-Options.X-Content-Type-Options.HTTP Strict Transport Se ...
- Error attempting to get column from result set
当使用mybatis plus3.2.0+springboot2.1.1 报错 Error attempting to get column from result set 1.一般出现这种问题,最简 ...
- JavaScript 中数组 sort() 方法的基本使用
在日常的代码开发中,关于数组排序的操作可不少,JavaScript 中可以调用 sort 方法对数组进行快速排序. 今天,就数组的 sort 方法来学习一下,避免日后踩坑的悲惨遭遇. 概念 sort ...
- 智能物联网(AIoT,2020年)(中)
智能物联网(AIoT,2020年)(中) 05 中国AIoT产业图谱 06 中国AIoT商业模式 标准程度越低人力和时间成本投入越多,2B2C模式附加值高 07 中国AIoT玩家分布简介 四类玩家,优 ...
- 单点突破:MySQL之基础
前言 开发环境:MySQL5.7.31 本文并不是mysql语法语句的教程或者笔记,如果初学MySQL,想要看sql的教程或者学习各种语法语句规范,可以看看一千行MySQL学习笔记或者MySQL教程| ...
- hashmap专题
hashmap重要变量 源码中定义了很多常量,有几个是特别重要的. DEFAULT_INITIAL_CAPACITY: Table数组的初始化长度: 1 << 4,即 2^4=16(这里可 ...