【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 ...
随机推荐
- strcmp实现
#include<stdio.h> #include<assert.h> int my_strcmp(const char*a,const char*b) { while(*a ...
- 我所遭遇过的游戏中间件--FlashOcx
使用Flash做游戏界面的另一种方式是通过Abode提供flash.ocx处理Flash界面.将Flash图像通过GDI绘制出来后,再将图像数据拷贝到一个D3D的纹理结构中,最后由引擎的D3D接口进行 ...
- 【属性动画总结】Property Animation
属性动画概述 3.0以前,android仅支持两种动画模式,tweened animation 和 frame-by-frame animation,在android3.0中又引入了一个新的动画系统: ...
- 解决 ASP.NET 编辑错误"CS0006: 未能找到元数据文件C:\WINDOWS\assembly\GAC_32\System.EnterpriseServices\2.0.0.0__b03f5f7f11d50a3a\System.EnterpriseServices.dll"
问题背景: 公司最近给我配置了一台新Windows 7旗舰版的电脑,这几天一直在迁移文件,因为新电脑上安装Sqlserver r2失败,解决方法是要安装一个800+MB的安装包 由于最近手上事情比较多 ...
- 整数划分问题--DFS
单点时限:1000ms 内存限制:256MB 描写叙述 Given two positive integers N and M, please divide N into several intege ...
- C语言高速入门系列(二)
C语言高速入门系列(二) -----转载请注明出处coder-pig 本节引言: 在前面一节中我们对C语言进行了初步的了解,学会了使用IDE进行代码的编写,编译执行! 在这一节中我们会对C语言的基本的 ...
- linux的fork()函数-进程控制
进程作为构成系统的基本细胞,不仅是系统中独立活动的实体,而且是独立竞争资源的基本实体.它要经历创建.执行.等待.终止等一系列过程. 一.fork入门知识(转载) 一个进程,包括代码.数据和分配给进程的 ...
- 深入理解 Linux 内存管理
1. 内存地址 以Intel的中央处理器为例,Linux 32位的系统中.物理内存的基本单位是字节(Byte),1个字节有8个二进制位. 每一个内存地址指向一个字节,内存地址加1后得到下一个字节的地址 ...
- 安装ubuntu后不能从ubuntu引导修复方法
sudo fdisk -l sudo -i mkdir /media/tempdir mount /dev/sda7 /media/tempdir grub-install --root-direct ...
- gzip和zipfile模块
# -*- coding: utf-8 -*- #python 27 #xiaodeng #gzip和zipfile模块 #http://www.open-open.com/lib/view/open ...