LeetCode 032 Longest Valid Parentheses
题目描述: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的更多相关文章
- [LeetCode] 032. Longest Valid Parentheses (Hard) (C++)
指数:[LeetCode] Leetcode 指标解释 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 032. Lon ...
- Java for LeetCode 032 Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- LeetCode 之 Longest Valid Parentheses(栈)
[问题描写叙述] Given a string containing just the characters '(' and ')', find the length of the longest v ...
- [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】Longest Valid Parentheses
Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...
- 【leetcode】 Longest Valid Parentheses (hard)★
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- Java [leetcode 32]Longest Valid Parentheses
题目描述: Given a string containing just the characters '(' and ')', find the length of the longest vali ...
- [leetcode]32. Longest Valid Parentheses最长合法括号子串
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
随机推荐
- 这些C++基础知识的基础知识你都学会了吗?
一.C++基础知识 新的数据类型 C语言中的数据类型 C++中新的数据类型 思考:新的数据类型有什么好处?请看下面的代码: 可以见得:新的类型使整个程序更加简洁,程序变得易读易懂!这个就是bool ...
- Python图像读写方法对比
训练视觉相关的神经网络模型时,总是要用到图像的读写.方法有很多,比如matplotlib.cv2.PIL等.下面比较几种读写方式,旨在选出一个最快的方式,提升训练速度. 实验标准 因为训练使用的框架是 ...
- 今天的一个SQL题-case语句和decode函数
数据库表: select * from rec order by rst,game_time; ID GAME_TIME RST ------ -------------- ---- 2 0 ...
- 探索 .Net Core 的 SourceLink
介绍 Source Link是一组软件包和一个规范, 它将一些元数据添加到PDB文件,以将本地文件重新映射到GitHub上的文件,因此Visual Studio可以在这需要时下载文件, 该项目的目的是 ...
- prometheus函数介绍
一 函数介绍 gauge类型的数据 属于随机变化数值,并不像counter那样 是 持续增长 1 increase() increase 函数 在promethes中,是⽤来 针对Counter 这 ...
- psycopg2模块安装问题
我的平台是win10(x64).python3.7,打算通过psycopg2模块来操作Greenplum数据库,我通过pip install psycopg2 安装了psycopg2模块,也提示安装成 ...
- gcc编译链接用到的环境变量
PATH ---- 可执行程序寻找路径 C_INCLUDE_PATH ---- 头文件寻找路径 CPLUS_INCLUDE_PATH --- g++ 头文件路径 LD_LIBRARY_PATH ...
- CephFS用户认证格式写错的问题
问题三: CephFS(James Gallagher) 问题原文 Hi, I'm looking to implement the CephFS on my Firefly release (v0. ...
- Java中List,Set,Map区别
在Java开发面试中,面试官最常问到的就是Java集合,免不了要让面试者说出之间的区别,下面博主就对其做了总结. 1.集合与数组的区别 长度区别:数组是固定长度,集合长度可变: 内容区别:数组可以是基 ...
- arm-linux openssl移植
从openssl官网下载openssl https://www.openssl.org/source/ 1.解压到linux中 2. ./config no-asm shared --prefix=$ ...