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的更多相关文章

  1. [LeetCode] 032. Longest Valid Parentheses (Hard) (C++)

    指数:[LeetCode] Leetcode 指标解释 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 032. Lon ...

  2. Java for LeetCode 032 Longest Valid Parentheses

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  3. LeetCode 之 Longest Valid Parentheses(栈)

    [问题描写叙述] Given a string containing just the characters '(' and ')', find the length of the longest v ...

  4. [LeetCode] 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】Longest Valid Parentheses

    Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...

  7. 【leetcode】 Longest Valid Parentheses (hard)★

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  8. Java [leetcode 32]Longest Valid Parentheses

    题目描述: Given a string containing just the characters '(' and ')', find the length of the longest vali ...

  9. [leetcode]32. Longest Valid Parentheses最长合法括号子串

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  10. LeetCode 032 Longest Valid Parentheses

    题目描述:Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the l ...

随机推荐

  1. Dubbo、Zookeeper、SpringMVC的整合使用

    互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,Dubbo是一个分布式服务框架,在这种情况下诞生的.现在核心业务抽取出来,作为独立的服务,使 ...

  2. 为 Python Server Pages 和 Oracle 构建快速 Web 开发环境。

    为 Python Server Pages 和 Oracle 构建快速 Web 开发环境. - 在水一方 - 博客频道 - CSDN.NET 为 Python Server Pages 和 Oracl ...

  3. 在国内使用cnpm代替npm

    npm是Node.js的模块依赖管理工具,由于使用npm安装包是从国外服务器下载,在国内很容易受到网络的影响,速度非常慢,因此可以选用cnpm.cnpm可以使用淘宝团队提供的淘宝npm镜像,你可以用此 ...

  4. PHP MySQL Order By 关键词 之 Order By

    ORDER BY 关键词 ORDER BY 关键词用于对记录集中的数据进行排序. 语法 SELECT column_name(s) FROM table_name ORDER BY column_na ...

  5. Struts2(五)——核心拦截器

    Struts框架一共为我们提供了35个拦截器,其中默认的拦截器有18个,框架访问action的异常处理,配置信息处理,转发重定向选择,上传等等等等,都是这18个拦截器中设置的,起着非比寻常的作用.而这 ...

  6. Quartz 开源的作业调度框架

    Quartz 是一个开源的作业调度框架,它完全由 Java 写成,并设计用于 J2SE 和 J2EE 应用中.它提供了巨大的灵活性而不牺牲简单性.你能够用它来为执行一个作业而创建简单的或复杂的调度.本 ...

  7. CHARINDEX (Transact-SQL)

    SQL Server 2014 其他版本 2(共 3)对本文的评价是有帮助 - 评价此主题 在一个表达式中搜索另一个表达式并返回其起始位置(如果找到). Transact-SQL 语法约定 语法   ...

  8. hive优化之自己主动合并输出的小文件

    1.先在hive-site.xml中设置小文件的标准. <property> <name>hive.merge.smallfiles.avgsize</name> ...

  9. 【二分答案】【POJ3122】【Northwestern Europe 2006】Pie

    Pie Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10629   Accepted: 3744   Special Ju ...

  10. 高效批量更新 sql 字段的值(从一个表向另一个表更新)

    里给出一种更高效.简洁的做法,批量更新SQL ,一句SQL就可以替代麻烦的循环过程,有MS SQLServer.Oracle.DB2下的写法--关键点:t4和t1是同一个table,primary k ...