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. SQL2005以上行变行简单实现

    用语法PIVOT参照:http://technet.microsoft.com/zh-cn/library/ms177410(v=sql.105).aspx

  2. A Tour of Go Making slices

    Slices are created with the make function. It works by allocating a zeroed array and returning a sli ...

  3. Docker - 配置国内加速器加速镜像下载。

    引言 由于网络原因,我们在pull Image 的时候,从Docker Hub上下载会很慢...所以,国内的Docker爱好者们就添加了一一些国内的镜像(mirror),方便大家使用. 配置阿里云加速 ...

  4. 团队项目·冰球模拟器——cmake 自动化构建系统的配置文件的编写

    1 前言 考虑到命令行界面下编译程序并不如在 IDE 那么直观,再考虑到各位队友对 Linux 并不熟悉,如何大幅度地减轻整个项目的开发复杂度就是一个很重要的问题. 在 Linux 下有个很古老但很有 ...

  5. C# ip hash算法实现ip分流

    private void button42_Click(object sender, EventArgs e) { Dictionary<int, string> proxyIpNodes ...

  6. iOS-iPhone系统版本号-iPhone App版本号

    转载: http://blog.sina.com.cn/s/blog_7b9d64af0101bu9j.html 很多时候,我们需要获得用户iPhone版本号,或者App的当前版本号. 关心以下两个方 ...

  7. 为一张PCI卡打通经络的过程

    一张PCI卡通过“一转二”的转接卡插在主板上,probe调用失败,日志显示读取配置空间的时候发生了奇偶校验错误,可是使用相同的转接卡把它插在另外一台相同机器的主板上时,却运行正常,这就说明不是转接卡的 ...

  8. 常用加密算法的Java实现(一) ——单向加密算法MD5和SHA

    1.Java的安全体系架构 1.1           Java的安全体系架构介绍 Java中为安全框架提供类和接口.JDK 安全 API 是 Java 编程语言的核心 API,位于 java.sec ...

  9. java中Map等对象转换为json

    ObjectMapper objectMapper = new ObjectMapper(); String jsonString = objectMapper.writeValueAsString( ...

  10. css 常见兼容性问题及解决方案

    css 兼容问题一直是困扰前端开发人员的大难题,提到兼容性立马想到了万恶的ie6,说多了都是泪,还是整理一些常见的兼容性问题以及解决的方案吧. 一. 浮动元素双边距. ①条件:ie6下,如果给元素设置 ...