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. Oracle--SQL Developer创建连接及使用

    安装好Oracle之后,有几种方式可以来管理Oracle中的数据库,首先就是登陆网页版的界面:https://localhost:1158/em,这种方式管理的东西太多,使用起来有点不方便,第二种方式 ...

  2. Windows下Apache+Tomcat+jsp+php的服务器整合配置经验总结

    对于Apache+Tomcat+jsp+php的整合,针对不同的Tomcat和apache的版本,稍微有些区别. 一.所需软件 (1)JDK: jdk-7u15-windows-x64.exejdk的 ...

  3. 关于AFinal的混淆

    http://blog.csdn.net/aibieqing241/article/details/38979531 我的前个项目中打算引用个框架,于是就看上了AFinal的这个开源库, 感觉用着还不 ...

  4. Eclipse reports that Android SDK Content Loader has encountered a problem. parseSdkContent failed.

    1) Download the SDK platform for API 20 (4.4W) 2) Navigate to your sdk folder (should be like D:\Ecl ...

  5. Windows下安装破解JIRA6.3.6

    相关工具下载地址:http://pan.baidu.com/s/1kT9xZEJ 安装环境: WindowsXP MySQL-5.5.28 JDK1.6.0_21 JIRA功能全面,界面友好,安装简单 ...

  6. cglib源码分析(四):cglib 动态代理原理分析

    本文分下面三个部分来分析cglib动态代理的原理. cglib 动态代理示例 代理类分析 Fastclass 机制分析 一.cglib 动态代理示例 public class Target{ publ ...

  7. 解锁Dagger2使用姿势(二) 之带你理解@Scope

    关于Dagger2使用的基础如果你还不了解,可以参考我的上一篇文章解锁Dagger2使用姿势(一),这有助于你理解本篇文章. OK,我们在上篇文章介绍另外Dagger2使用过程中四个基本的注解,分别是 ...

  8. input 的 placeholder属性在IE8下的兼容处理

    placeholder是input标签的新属性,在使用的时候有两个问题: 1.IE8 下不兼容 处理思路: 如果浏览器不识别placeholder属性,给input添加类名placeholder,模仿 ...

  9. Swift之语法1(精简版)

    1. Swift简介 2010年的夏天,苹果公司的开发人员Chris Lattne接到了一个特别的任务,为OS X 和iOS平台开发下一代的编程语言,也就是Swift. 苹果公司于2014年WWDC( ...

  10. 解决PL/SQL Developer中文乱码的问题

    set nls_lang=SIMPLIFIED CHINESE_CHINA.ZHS16GBK cd "c:\Program Files\PLSQL Developer" PLSQL ...