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

  1. [LeetCode] 32. Longest Valid Parentheses 最长有效括号

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

  2. leetcode 32. Longest Valid Parentheses

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

  3. Java [leetcode 32]Longest Valid Parentheses

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

  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(最长合法的括号组合)

    题目链接: https://leetcode.com/problems/longest-valid-parentheses/?tab=Description   Problem :已知字符串s,求出其 ...

  6. [LeetCode] 32. Longest Valid Parentheses (hard)

    原题链接 题意: 寻找配对的(),并且返回最长可成功配对长度. 思路 配对的()必须是连续的,比如()((),最长长度为2:()(),最长长度为4. 解法一 dp: 利用dp记录以s[i]为终点时,最 ...

  7. [Leetcode][Python]32: Longest Valid Parentheses

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 32: Longest Valid Parentheseshttps://oj ...

  8. leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、

    20. Valid Parentheses 错误解法: "[])"就会报错,没考虑到出现')'.']'.'}'时,stack为空的情况,这种情况也无法匹配 class Soluti ...

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

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

  10. 刷题32. Longest Valid Parentheses

    一.题目说明 题目是32. Longest Valid Parentheses,求最大匹配的括号长度.题目的难度是Hard 二.我的做题方法 简单理解了一下,用栈就可以实现.实际上是我考虑简单了,经过 ...

随机推荐

  1. 记一个万金油开源框架JHipster

    本文地址:http://blog.csdn.net/sushengmiyan/article/details/53190236 百搭代码生成框架 体验新技术汇总: Spring Boot Spring ...

  2. RunLoop总结:RunLoop 与GCD 、Autorelease Pool之间的关系

    如果在面试中问到RunLoop相关的知识,很有可能也会问到RunLoop与GCD.Autorelease Pool有没有关系,哪些地方用到了GCD.Autorelease Pool等. So,本文就总 ...

  3. VMware 下的CentOS6.7 虚拟机与Windows7通信

    在有网络的情况下,VMware 虚拟机使用桥接模式(Bridged) 和NAT方式,会自动通信,但是在没有网络的情况下怎么办呢?对,是的,使用host-only模式,如何设置呢? 注:将Windows ...

  4. 1.物理系统PhysicsWorld,RayCast

     1 3.0物理系统PhysicsWorld T07PhysicsWorld.h #ifndef __T07PhysicsWorld_H__ #define __T07PhysicsWorld_H ...

  5. 视频特性TI(时间信息)和SI(空间信息)的计算工具:TIandSI-压缩码流版

    ===================================================== TI(时间信息)和SI(空间信息)计算工具文章列表: 视频特性TI(时间信息)和SI(空间信 ...

  6. Android动态换肤(二、apk免安装插件方式)

    在上一篇文章Android动态换肤(一.应用内置多套皮肤)中,我们了解到,动态换肤无非就是调用view的setBackgroundResource(R.drawable.id)等方法设置控件的背景或者 ...

  7. Github Pages 搭建HEXO主题个人博客

    跌跌撞撞,总算是建立起来了.回首走过的这么多坑,也真的是蛮不容易的.那么就写点东西,记录我是怎么搭建的吧. 准备工作 安装Node.js: 用于生成静态页面,我们需要到官网上去下载即可.http:// ...

  8. ROS(indigo)MoveIt!控制ABB RobotStudio 5.6x 6.0x中机器人运动

    Gazebo以及相关参考文献,参考: ROS(indigo)ABB机器人MoveIt例子 这里需要配置RobotStudio,请参考ROS官网教程.下面列出要点:   window端配置结束后,在Ub ...

  9. Android开发学习之路--Annotation注解简化view控件之初体验

    一般我们在写android Activity的时候总是会在onCreate方法中加上setContentView方法来加载layout,通过findViewById来实现控件的绑定,每次写这么多代码总 ...

  10. jQuery Ajax 使用 ($.ajax、$.post、$.get)

    项目中只要涉及到前后台的交互,数据状态之间的交互,ajax是必不可少的.一般项目中jquery方式的ajax用的还是比较多的.封装的比较好,用起来也顺手,兼容浏览器之间的差异. 操作的方式有三种: 1 ...