【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 ...
随机推荐
- tsort - 拓扑排序
tsort - 拓扑排序 本文链接:http://codingstandards.iteye.com/blog/834572 (转载请注明出处) 用途说明 tsort命令通常用于解决一种逻辑问题, ...
- 用java在客户端读取mongodb中的数据并发送至服务器
使用Java自带的socket端口来实现,程序如下: Client.java package com.cn.gao; import java.net.*; import java.io.*; impo ...
- mini-parser
https://leetcode.com/problems/mini-parser/ /** * // This is the interface that allows for creating n ...
- 三个和数组有关的程序题目(C++)
题目一:有n个整数,使前面各数顺序向后移动m个位置 问题描述: 有n个整数,使前面各数顺序向后移动m个位置,最后m个数变成最前m个数 程序代码: #include<iostream> us ...
- graphic rendering pipeline
整理下管线 此时一定要有这张图 注意表中的数据流向 强调几个细节 之前对次序理解有点乱 rasterizer之前 管线里是只有逐顶点信息的 IA里面会setup primitive 通过Primit ...
- 实现iframe窗口高度自适应的又一个巧妙思路
domainA 中有一个页面index.html,通过iframe嵌套了domainB中的一个页面other.html由于other.html页面在iframe中显示,而且其页面内容会动态的增加或减少 ...
- 为什么谷歌的JSON响应以while(1);开头?
问题(QUESTION): 我有个问题一直很好奇就是:为什么谷歌的JSON响应以while(1);开头?举个例子,当把谷歌日历打开和关掉时,会返回这样的JSON对象: while(1);[['u',[ ...
- 【leetcode】 9. palindrome number
@requires_authorization @author johnsondu @create_time 2015.7.13 9:48 @url [palindrome-number](https ...
- CF 463D Gargari and Permutations [dp]
给出一个长为n的数列的k个排列(1 ≤ n ≤ 1000; 2 ≤ k ≤ 5).求这个k个数列的最长公共子序列的长度 dp[i]=max{dp[j]+1,where j<i 且j,i相应的字符 ...
- Real-time 3D Reconstruction using Kinect
http://jiakaizhang.com/project/real-time-3d-reconstruction/ Real-time 3D Reconstruction using Kinect ...