【一天一道LeetCode】#32. Longest Valid Parentheses
一天一道LeetCode系列
(一)题目
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.
(二)解题
/*
stack法思想:和Valid Parentheses一样,用stack来解决,只不过栈中记录的是每个‘(’的下标
1.首先在栈里面压入初始值-1(初始匹配失败的下标), 如果是‘(’就直接把‘(’的下标压进栈
2. 如果是‘)’开始判断
(1) 如果此刻栈的大小大于1,则将栈顶元素弹出,将下标减去当前的栈顶元素即为当前有效串的长度
(2) 如果此刻栈的大小小于等于1,则代表匹配失败,更新目前匹配失败的下标。
*/
class Solution {
public:
int longestValidParentheses(string s) {
stack<int> stmp;
stmp.push(-1);
int max = 0;
for(int i = 0 ; i < s.length() ; i++){
if(s[i] == '(')
{
stmp.push(i);
}
else
{
if(stmp.size()>1)
{
stmp.pop();
int len = i - stmp.top();
max = max>len?max:len;
}
else
{
stmp.pop();
stmp.push(i);
}
}
}
return max;
}
};
【一天一道LeetCode】#32. Longest Valid Parentheses的更多相关文章
- [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 ...
- 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 ...
- LeetCode 32 Longest Valid Parentheses(最长合法的括号组合)
题目链接: https://leetcode.com/problems/longest-valid-parentheses/?tab=Description Problem :已知字符串s,求出其 ...
- [LeetCode] 32. Longest Valid Parentheses (hard)
原题链接 题意: 寻找配对的(),并且返回最长可成功配对长度. 思路 配对的()必须是连续的,比如()((),最长长度为2:()(),最长长度为4. 解法一 dp: 利用dp记录以s[i]为终点时,最 ...
- [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 ...
- [LeetCode] 032. Longest Valid Parentheses (Hard) (C++)
指数:[LeetCode] Leetcode 指标解释 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 032. Lon ...
- 刷题32. Longest Valid Parentheses
一.题目说明 题目是32. Longest Valid Parentheses,求最大匹配的括号长度.题目的难度是Hard 二.我的做题方法 简单理解了一下,用栈就可以实现.实际上是我考虑简单了,经过 ...
随机推荐
- Bootstrap3 排版-页面主体
Bootstrap 将全局 font-size 设置为 14px,line-height 设置为 1.428.这些属性直接赋予 元素和所有段落元素.另外,<p> (段落)元素还被设置了等于 ...
- linux2.6.37内核接两个硬盘导致读写效率变低的问题
一.问题分析: 通过跟踪定位write系统调用的实现发现,在每次调用a_ops->write_end之后,都会去调用balance_dirty_pages_ratelimited,该函数负责检查 ...
- 剑指Offer——知识点储备-Java基础
剑指Offer--知识点储备-Java基础 网址来源: http://www.nowcoder.com/discuss/5949?type=0&order=0&pos=4&pa ...
- Windows 8 Cython 的配置(解决Unable to find vcvarsall.bat问题)
关键是安装之前配置编译器. 1.下载MinGW 编译器 http://www.mingw.org/download.shtml 2.把编译器路径(例如C:\Program Files (x86)\Co ...
- Spark技术内幕:Shuffle Map Task运算结果的处理
Shuffle Map Task运算结果的处理 这个结果的处理,分为两部分,一个是在Executor端是如何直接处理Task的结果的:还有就是Driver端,如果在接到Task运行结束的消息时,如何对 ...
- SpriteKit游戏开发 Challenge 2: An invincible zombie 问题的另一种解决方法
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 该挑战的目的是僵尸碰到敌人时,将其设置为无敌模式,具体要求如下 ...
- SQLite 数据类型(http://www.w3cschool.cc/sqlite/sqlite-data-types.html)
SQLite 数据类型 SQLite 数据类型是一个用来指定任何对象的数据类型的属性.SQLite 中的每一列,每个变量和表达式都有相关的数据类型. 您可以在创建表的同时使用这些数据类型.SQLite ...
- How to speed up Remote Desktop Connection in Win7
run following command in DOS window: netsh interface tcp set global autotuninglevel=disabled or nets ...
- EBS开发常用编译命令
一.编译FORM 1.将脚本写成shell脚本 cd $AU_TOP/forms/ZHS export FORMS_PATH=.:$FORMS_PATH:$AU_TOP/forms/ZHS frmcm ...
- Android开发学习之路--RxAndroid之操作符
学习了RxAndroid的一些基本知识,上篇文章也试过了RxAndroid的map操作符,接着来学习更多的操作符的功能吧. 操作符就是为了解决对Observable对象的变换的问题,操作符用于 ...