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:

  从左到右依次扫描,用一个数组v记录括号是否已经匹配。然后再扫描一次v,用一个变量count记录连续合法括号数目。如果v[i] = 0则说明该括号未匹配,合法括号序列终端,count = 0. 代码如下:

Runtime: 22 ms

class Solution {
public:
int longestValidParentheses(string s) {
vector<int> v(s.length(), );
stack<int> stac;
for (int i = ; i < s.length(); ++i) {
if (s[i] == ')') {
if (stac.empty())
continue;
else {
v[i] = ;
v[stac.top()] = ;
stac.pop();
}
}
else
stac.push(i);
} int res = , count = ;
for (int i = ; i < s.length(); ++i) {
if (v[i] == ) {
if (res < count)
res = count;
count = ;
}
else if (s[i] == '(')
count += v[i];
}
return res < count ? count : res;
} };

思路二:

  先从左到右扫描,分别记录'('和')'的数目, 如果一旦')'的数目大于'(' 那么可以确定合法连续括号序列中断。记录countMax。

  然后从右往左扫描,同样分别记录'('和')'的数目, 如果一旦'('的数目大于')' , 那么可以确定合法连续括号序列中断。记录countMax。

代码如下:

Runtime: 10 ms

class Solution {
public:
int longestValidParentheses(string s) {
int ll = , lr = , li = ;
int rl = , rr = , ri = s.length()-;
int res = ;
for (; li < s.length() && ri >= ; ++li, --ri) {
switch (s[li]) {
case '(':
++ll;
break;
case ')':
++lr;
}
switch (s[ri]) {
case '(':
++rl;
break;
case ')':
++rr;
}
if (ll == lr && (ll * ) > res)
res = * ll;
else if (ll < lr)
ll = lr = ; if (rl == rr && (rl * ) > res)
res = * rl;
else if (rl > rr)
rl = rr = ; } return res;
}
private: };

leetcode problem 32 -- Longest Valid Parentheses的更多相关文章

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

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

  2. 【一天一道LeetCode】#32. Longest Valid Parentheses

    一天一道LeetCode系列 (一)题目 Given a string containing just the characters '(' and ')', find the length of t ...

  3. 【LeetCode】32. Longest Valid Parentheses (2 solutions)

    Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...

  4. 【LeetCode】32. Longest Valid Parentheses

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

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

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

  6. 刷题32. Longest Valid Parentheses

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

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

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

  8. leetcode 32. Longest Valid Parentheses

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

  9. Java [leetcode 32]Longest Valid Parentheses

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

随机推荐

  1. puppet运维配置实列

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABQkAAAGBCAIAAABKMKAEAAAgAElEQVR4nOydeXwU9cH/t2o9WutR+7

  2. Java同步块(synchronized block)使用详解

    Java 同步块(synchronized block)用来标记方法或者代码块是同步的.Java同步块用来避免竞争.本文介绍以下内容: Java同步关键字(synchronzied) 实例方法同步 静 ...

  3. hdu 2711&&poj2182 Lost Cows (线段树)

    从后往前查第一个为0的奶牛肯定应该排在第一个.每次从后往前找到第一个为0的数,这个数应该插在第j位.查找之后,修改节点的值为极大值,当整棵树的最小值不为0的时候查找结束. 至于这种查找修改的操作,再没 ...

  4. SRM659 1100pts

    绍一模拟赛的题[问题描述]小Z.小Y和小B拥有

  5. URAL 1994 The Emperor's plan 求组合数 大数用log+exp处理

    URAL 1994 The Emperor's plan 求组合数 大数用log #include<functional> #include<algorithm> #inclu ...

  6. JBPM学习(六):详解流程图

    概念: 流程图的组成: a. 活动 Activity / 节点 Node b. 流转 Transition / 连线(单向箭头) c. 事件 1.流转(Transition) a) 一般情况一个活动中 ...

  7. windows 文件名太长无法删除的解决方法

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...

  8. java17 线程的方法

    线程的方法: .isAlive():判断线程是否还活着,即线程是否还未中止. .getPriority():获得线程的优先级数值. .setPriority():设置线程的优先级. .setName( ...

  9. lscpu lsblk lsscsi lspci

    [root@server1 ~]# lscpu Architecture: x86_64 CPU op-mode(s): -bit, -bit Byte Order: Little Endian CP ...

  10. Using Sessions and Session Persistence---reference

    Using Sessions and Session Persistence The following sections describe how to set up and use session ...