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. JavaScript- The Good Parts function Curry

    Functions are values, and we can manipulate function values in interesting ways.Currying allows us t ...

  2. hdu 3724 Encoded Barcodes

    Trie模板.把所有单词都用字典树存起来,然后给每个节点赋权值表示前缀到该节点出现了几次.然后直接查询就可以了. #include <algorithm> #include <ios ...

  3. 非常实用的Android Studio快捷键

    One—打印Log 生成tag: logt 打印log: logm logd loge Two—代码提示 Ctrl + Alt + Space Three—代码移动 选中代码: Ctrl + w 向 ...

  4. GridControl 复合表头(多行标题)

    说明: 最好是通过编辑视图进行设计,后台编码有点麻烦. 例图:(上面的GC是后台编写 ,下面的是设计器设计) 后台代码编写: public void InitCtrl() { DevExpress.X ...

  5. JDK之jstat的用法

    http://www.51testing.com/html/92/77492-203728.html jstat的用法 用以判断JVM是否存在内存问题呢?如何判断JVM垃圾回收是否正常?一般的top指 ...

  6. [转]Reducing script compile time or a better workflow to reduce excessive recompiling

    http://forum.unity3d.com/threads/148078-Reducing-script-compile-time-or-a-better-workflow-to-reduce- ...

  7. hdu1250(Java)大数相加的问题

    Hat's Fibonacci Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...

  8. javascript进击(一)简介

    javascript是属于网络的脚本语言(javascript与java就像老婆与老婆饼,并没有关系) 页面静态效果:HTML+CSS 为页面添加动态效果:javascript JavaScript ...

  9. Scala函数字面量简化写法

    Scala提供了多种方法来简化函数字面量中多余的部分,比如前面例子中filter方法中使用的函数字面量,完整的写法如下: (x :Int ) => x +1 首先可以省略到参数的类型,Scala ...

  10. Android Studio之could not reserve enough space for object heap

    在用AndroidStudio时出现这样的错误:  每次创建工程后,在项目文件 gradle.properties文件中加入如下代码: org.gradle.jvmargs=-Xmx512m -XX: ...