【LeetCode练习题】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 str) {
int maxLen = ;
int count = ;
stack<int> s;
int firstLeft = ;
for(int i = ; i < str.size(); i++){
//i代表当前的下标
if(str[i] == '('){
s.push(i); // 遇到( 就push
}
else{
//str[i] == ')'
if(!s.empty()){
//栈非空
s.pop();
if(!s.empty()){
//pop后栈里还有元素,比如"()(()()"来举例
int lastIndex = s.top(); //lastIndex 是中间那个( 的下标,即为2
int len = i - lastIndex; // 此时 i = 4或者 6,以6举例的话, len等于4
if(len > maxLen) // if ( 4 > 2) true maxLen = 4
maxLen = len;
}
else{
//pop后没有元素了,比如"(())"情况举例
int len = i - firstLeft + ; //此时firstLeft等于0,i 等于3的话,len等于 3 - 0 + 1 = 4
if(len > maxLen){
maxLen = len;
}
}
}
else{
firstLeft = i + ; //栈为空的时候遇到),将firstLeft移到i 的下一个。
}
}
}
return maxLen;
}
};
解题思路:
因为存在类似于"()(()()"的情况,如果我们仅仅只是遇到(就压栈,遇到)就弹栈的话,然后通过一个count和一个maxLen来计算当前的最大长度,就会遇到问题。
因为第二个"("是到最后也没有匹配到的,他应该是将左边和右边的子串分割开来了,即左边的长度为2,右边的长度为4。可如果按照我之前的那个只有当栈为空且遇到的是")"的时候字符串才出现分割的想法的话,“()(()()”的长度就是6了,因为他一直都没有被分割。
那么,我们如何来标记那些已经压进栈里的却没有得到")"匹配,起着分割左右子串作用的"("符号呢?
这样,我们的stack的元素类型不是char 了,不存"(",")"这样的字符,而是存下每一个"("字符的下标,是int类型。
当遇到一个")"而且栈不为空的时候,弹栈。当弹栈之后发现栈还是不为空的时候,此时栈顶的那个元素lastIndex即为没有匹配的那个"(" 的下标值了,我们用此时的下标 i 和lastIndex的差值就知道了真正的有效括号的长度,再将他和maxLen比较,让maxLen等于他们中的较大值。
【LeetCode练习题】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 ...
- LeetCode 032 Longest Valid Parentheses
题目描述:Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the l ...
随机推荐
- Mysql.Data的连接驱动 .net 的源码竟然在git了
如标题 上链接:https://github.com/mysql/mysql-connector-net
- mvc和三层架构到底有什么区别
原文地址:http://zhidao.baidu.com/question/82001542.html?qbl=relate_question_3&word=MVC%20%CA%FD%BE%D ...
- 动态创建和移除HTML标签
1.w3school定义 添加新内容的四个 jQuery 方法: append() - 在被选元素的结尾插入内容 prepend() - 在被选元素的开头插入内容 after() - 在被选元素之后插 ...
- poj 1703(带权并查集)
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 31840 Accepted: ...
- MongoDB基本命令随便敲敲
1,mongoDB状态,版本,当前连接的数据库名称
- Python常用模块(time, datetime, random, os, sys, hashlib)
time模块 在Python中,通常有这几种方式来表示时间: 时间戳(timestamp) : 通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量.我们运 ...
- 一款超出你想象的代码审阅软件understand
看源码人们一般会想到source insight这款软件可是这款软件目前只支持windows平台,那如果我想在Linux平台上审阅代码呢, 没关系还有一款强大的软件understand,这款软件能够生 ...
- Unity 扩展属性自定义绘制
这么晚了准备睡觉的时候,去学习了一会. 发现一个标题好奇的点进去. 居然是自定义绘制属性. 在前几天这个问题把我难住了,没想到几分钟就能解决的问题. 我花了半天时间使用反射去解决... 如果我们想 ...
- pom.xml详解(转)
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
- 详细介绍Qt,ffmpeg 和SDL开发
Qt 与 ffmpeg 与 SDl 教程是本文要介绍的内容,从多个角度介绍本文,运用了qmake,先来看内容. 1. 注释 从“ #” 开始,到这一行结束. 2. 指定源文件 1. ...