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. Android APP开发需求文档范本

    Android  APP开发需求文档范本 软件需求文档格式的标准写法 1.引言 1.1 编写目的 • 阐明开发本软件的目的: 1.2 项目背景 • 标识待开发软件产品的名称.代码: • 列出本项目的任 ...

  2. iOS开发之property属性介绍

    大家都知道@property和@synthesize可以自动生成某个类成员变量的存取方法,但可能对property中的一些属性不是很了解,网上的一些介绍有的不是很正确,感觉会误导新手,于是准备详细介绍 ...

  3. UESTC_菲波拉契数制 2015 UESTC Training for Dynamic Programming<Problem E>

    E - 菲波拉契数制 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submi ...

  4. UVA 11045-My T-shirt suits me(二分图匹配)

    题意:有N件T恤,N是6的倍数,因为有6种型号,每种件数相同,有M个人,每个人有两种型号的T恤适合他,每个人可以挑其中的一种,问能否所有的人都能分配到T恤. 解析:典型的二分图匹配,每N/6为同种T恤 ...

  5. HTTP学习笔记

    最近在看HTTP权威指南, 然后准备从Python的request库入手,看它的源代码实现 http://cn.python-requests.org/zh_CN/latest/ 挖坑 今年准备在gi ...

  6. oninput,onpropertychange,onchange的使用方法和差别

    1.前言 因为工作须要,需实现一个相似于微博输入框的功能,在用户动态输入文字的时候,改动提示“您还能够输入XX字”.例如以下图所看到的: 因此,略微研究了一下oninput,onpropertycha ...

  7. VLC源码分析知识总结

    1.  关于#和## 1.1).在C语言的宏中,#的功能是将其后面的宏参数进行字符串化操作(Stringfication),简单说就是在对它所引用的宏变量通过替换后在其左右各加上一个双引号. 比如在早 ...

  8. C# winfrom 模拟ftp文件管理

    从网上找到的非常好用的模拟ftp管理代码,整理了一下,希望对需要的人有帮助 using System; using System.Collections.Generic; using System.T ...

  9. 将json格式日期(毫秒数)转成日常日期格式和日常格式时间对比

    第一:是把生成的Json格式的时间转换,注意要看清楚时间的格式 function (cellval) { var date = new Date(parseInt(cellval.replace(&q ...

  10. 天坑 之 java web servlet+jsp项目 配置后 404 (MyEclipse转eclipse)

    最近搞一个自己的博客系统玩,用了servlet+jsp,结果发现了两个大问题: 1.无法 Export 出 WAR文件: 2.生成WAR,放置到TOMCAT的 webapps目录后,http://lo ...