【LeetCode】32. Longest Valid Parentheses (2 solutions)
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.
括号匹配的常规思路就是用进出栈。
但是这题的关键在于“连续匹配”,因此计算“连续匹配”长度时,本质就是求不匹配的括号之间最大长度。
也就是求进栈但未出栈的括号下标之间的最大差值。
注意边界:
(1)第一个留在栈中的括号之前的连续匹配长度。
(2)最后一个留在栈中的括号之后的连续匹配长度。
解法一:栈中记录未匹配括号的下标。在全部扫描s之后,将未匹配的括号逐个出栈进行计算。
class Solution {
public:
int longestValidParentheses(string s) {
int ret = ;
stack<int> stk; //store the indexes of unmatched parentheses
for(int i = ; i < s.size(); i ++)
{
if(s[i] == '(')
//unmatch
stk.push(i);
else
{//s[i] == ')'
if(!stk.empty() && s[stk.top()] == '(')
//match
stk.pop();
else
//unmatch
stk.push(i);
}
}
if(stk.empty())
//all match
ret = s.size();
else
{//check every unmatched pair of parentheses
int start;
int end = s.size()-;
while(!stk.empty())
{
start = stk.top();
stk.pop();
ret = max(ret, end-start);
end = start-;
}
//from begin to the first unmatched parenthese
ret = max(ret, end+);
}
return ret;
}
};
解法二:栈中记录括号及下标。在每次进站时计算与上个未匹配括号的距离。
struct Par
{
char c;
int ind;
Par(char newc, int newind): c(newc), ind(newind) {}
}; class Solution {
public:
int longestValidParentheses(string s) {
if(s == "")
return ; stack<Par> stk;
int ret = ;
int ind;
for(int i = ; i < s.size(); i ++)
{
if(s[i] == '(')
{
if(!stk.empty())
// distance between new unmatched parenthese and last unmatched parenthese
ret = max(ret, i-stk.top().ind-);
else
// distance between string begin and first unmatched parenthese
ret = max(ret, i);
Par p(s[i], i);
stk.push(p);
}
else if(s[i] == ')')
{
if(!stk.empty())
{
if(stk.top().c == '(')
stk.pop();
else
{// distance between new unmatched parenthese and last unmatched parenthese
ret = max(ret, i-stk.top().ind-);
Par p(s[i], i);
stk.push(p);
}
}
else
{// distance between string begin and first unmatched parenthese
ret = max(ret, i);
Par p(s[i], i);
stk.push(p);
}
}
}
if(stk.empty())
{//all matched
return s.size();
}
// distance between string end and last unmatched parenthese
ret = max(ret, (int)s.size()-stk.top().ind-); return ret;
}
};
【LeetCode】32. Longest Valid Parentheses (2 solutions)的更多相关文章
- 【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
一天一道LeetCode系列 (一)题目 Given a string containing just the characters '(' and ')', find the length of t ...
- 【Python】32. Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- [Leetcode][Python]32: Longest Valid Parentheses
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 32: Longest Valid Parentheseshttps://oj ...
- leetcode problem 32 -- Longest Valid Parentheses
Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...
- 【LeetCode】14. Longest Common Prefix (2 solutions)
Longest Common Prefix Write a function to find the longest common prefix string amongst an array of ...
- leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、
20. Valid Parentheses 错误解法: "[])"就会报错,没考虑到出现')'.']'.'}'时,stack为空的情况,这种情况也无法匹配 class Soluti ...
- 刷题32. Longest Valid Parentheses
一.题目说明 题目是32. Longest Valid Parentheses,求最大匹配的括号长度.题目的难度是Hard 二.我的做题方法 简单理解了一下,用栈就可以实现.实际上是我考虑简单了,经过 ...
- 【LeetCode】522. Longest Uncommon Subsequence II 解题报告(Python)
[LeetCode]522. Longest Uncommon Subsequence II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemin ...
随机推荐
- GNU GRUB
Introduction GNU GRUB is a Multiboot boot loader. It was derived from GRUB, the GRand Unified Bootlo ...
- poj 1201 Intervals 解题报告
Intervals Time Limit: 2000MS Memory Limit: 65536KB 64bit IO Format: %lld & %llu Submit Statu ...
- 开源项目-SlideMenu和actionbarsherlock的配置
SlidingMenu 是github上一个非常优秀的开源库,利用它可以很方便的实现左右侧滑菜单的效果,现在这个基本上应用的标配了,如果一个App没有滑动效果基本上是不可能的,中国人都是本着人无我有, ...
- 倒计时实现方案总结 Timer Handler
利用Timer实现倒计时 @BindView(R.id.send) Button send;//发送验证码 private int time = 60;//倒计时 private Timer time ...
- 判断 iframe 是否加载完毕
我能想到的有以下几种方式: 方法一.jQuery load() var frm = document.getElementById('myiframe'); $(frm).load(function( ...
- [Javascript Crocks] Make your own functions safer by lifting them into a Maybe context
You probably have functions that aren’t equipped to accept a Maybe as an argument. And in most cases ...
- Java之对象构造过程
先来运行一段代码 class A { public A() { init(); } public void init() { } public static void main(String[] ar ...
- 在陌生Linux环境查看Tomcat服务的方法
1.查看Tomcat进程 执行命令$ps -ef|grep tomcat 你就能找出tomcat占据的进程号,当然这要求tomcat启动了. # ps -ef | grep tomcatroot ...
- 相似qq的IM聊天应用源代码
这个是IM聊天应用源代码,该应用IM支持实现XMPP,以及图片和表情,语音.消息回执等功能,基本覆盖了常见的im应用的功能了,大家能够參考一下吧. 源代码下载:http://code.662p.com ...
- 缺少dll文件的解决方法
1.什么是dll文件 从专业的角度来说,dll文件,即动态连接库,是一种不可执行的二进制文件,它允许程序共享执行特殊任务所必需的代码和其他资源.打个比方,相当于你去饭店吃饭,只人带上钱或卡就可以了,不 ...