leetCode练习(32)
来源:互联网 发布:mac无法安装pkg 编辑:程序博客网 时间:2024/06/05 16:37
题目:Longest Valid Parentheses
难度:hard
问题描述:
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,一个boolean数组temp代表s的每个字符是否有与之相匹配的字符
每当遇到‘(‘,我们将其索引压入stack,当遇到’)‘,从stack中提出上一个‘(’的索引,将这两个索引的temp[i] [j]都设为ture。最后找到temp中最长的连续true即可。
具体代码如下:
0 0
- leetCode练习(32)
- leetCode练习(7)
- leetCode练习(9)
- leetCode练习(2)
- leetCode练习(14)
- leetCode练习(3)
- leetCode练习(4)
- leetCode练习(19)
- leetCode练习(18)
- leetCode练习(5)
- leetCode练习(15)
- leetCode练习(20)
- leetCode练习(21)
- leetCode练习(16)
- leetCode练习(17)
- leetCode练习(11)
- leetCode练习(24)
- leetCode练习(22)
- 圆角button,simpleAdapter ,ArrayAdapter,CheckBox背景图片
- android应用APP常见安全问题
- 使用OpenGL实现遮罩效果
- 带你读开源—ASP.NET_MVC(十五)
- 数据结构中的二叉搜索树
- leetCode练习(32)
- 利用阿里云提供的镜像快速更换本地的yum源
- WPF教程(六) XAML基础
- Raising Bacteria 细菌繁殖
- 抑郁症患者眼中的世界与自己是怎样的?
- 13个实用的Apache Rewrite重写规则
- poj 2941 Homogeneous Squares 解题报告
- 最优化理论中的常用背景知识
- Android studio 修改工程名、包名