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.

问题:求最长有效括号子字符串。

解题思路:

第一次做,以为是求整个字符串的有效括号长度是多少,思考了一会用 stack 可以求到,心想好像蛮简单的。扔到 LeetCode 跑了一下,结果错误,才发下原来是求 连续的最长有效括号长度。

重新理解题意后,开始做第二次。

想象括号匹配,就像玩那种 “天天爱消除” 新游戏,相邻两个字符,左边为 '(', 右边为 ')',则匹配成功,匹配成功的字符被替换为 '.'。

通过最多 n/2 次遍历,就可以把 s 中全部有效括号替换为 '.', 然后统计下连续 '.' 个数,就是题目的解,耗时 O(n*n)。实现后,扔到 LeetCode 上面,居然通过了,就是慢了一些。

再思考有没有更快的方法了,联想到第一次做用的 stack,可以借助 stack 一次遍历就将全部有效括号替换为 '.'。

第一步:一次遍历,stack 只存放 '(' 的下标。

    当找到一个 '(',则压进 stack ;

    当找到一个 ')',则把 stack.top 对于的字符替换为 '.',并弹出 stack.pop()。耗时O(n)。

第二步:求出连续 '.' 的最长长度, 耗时O(n)。

     stack<int> sk;

     for (int i = ; i < s.size(); i++) {
if (s[i] == ')') {
if (sk.empty()) {
continue;
}else{
int idx = sk.top();
sk.pop();
s[idx] = marked;
s[i] = marked;
}
}else{
// s[i] is '(' sk.push(i);
}
} int len = ;
int maxL = ;
for (int i = ; i < s.size(); i++) {
if (s[i] == '.') {
len++;
}else{
maxL = max(maxL, len);
len = ;
}
}
maxL = max(maxL, len); return maxL;
 

[LeetCode] Longest Valid Parentheses 解题思路的更多相关文章

  1. LeetCode: Longest Valid Parentheses 解题报告

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

  2. [Leetcode] longest valid parentheses 最长的有效括号

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

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

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

  4. [LeetCode] Longest Valid Parentheses

    第一种方法,用栈实现,最容易想到,也比较容易实现,每次碰到‘)’时update max_len,由于要保存之前的‘(’的index,所以space complexity 是O(n) // 使用栈,时间 ...

  5. [LeetCode] Longest Valid Parentheses 动态规划

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

  6. [LeetCode] Longest Valid Parentheses -- 挂动态规划羊头卖stack的狗肉

    (Version 1.3) 这题在LeetCode上的标签比较有欺骗性,虽然标签写着有DP,但是实际上根本不需要使用动态规划,相反的,使用动态规划反而会在LeetCode OJ上面超时.这题正确的做法 ...

  7. leetcode: Longest Valid Parentheses分析和实现

    题目大意:给出一个只包含字符'('和')'的字符串S,求最长有效括号序列的长度. 很有趣的题目,有助于我们对这种人类自身制定的规则的深入理解,可能我们大多数人都从没有真正理解过怎样一个括号序列是有效的 ...

  8. leetcode Longest Valid Parentheses python

    class Solution(object): def longestValidParentheses(self, s): """ :type s: str :rtype ...

  9. LeetCode解题报告—— Longest Valid Parentheses

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

随机推荐

  1. play app to war

    project/Build.scala import sbt._ import Keys._ import play.Play.autoImport._ import PlayKeys._ impor ...

  2. Oracle计算时间函数(numtodsinterval、numtoyminterval)

    numtodsinterval(<x>,<c>) ,x是一个数字,c是一个字符串,表明x的单位,这个函数把x转为interval day to second数据类型 常用的单位 ...

  3. Oracle: Oracle行转列、列转行的Sql语句总结

    例子原型: ' ; 运行结果如下: 一.多字段的拼接 将两个或者多个字段拼接成一个字段: ' ; 运行结果: 二.行转列 将某个字段的多行结果,拼接成一个字段,获取拼接的字符串[默认逗号隔开] ' ; ...

  4. Object layout in C++ and access control

    The variables are guaranteed to be laid out contiguously, as in C. However, the access blocks may no ...

  5. OSPF + LVS ,突破LVS瓶颈 (转)

    突破LVS瓶颈,LVS Cluster部署(OSPF + LVS) 前言 架构简图 架构优势 部署方法 1.硬件资源准备 2.三层设备OSPF配置 3.LVS调度机的OSPF配置 a.安装软路由软件q ...

  6. Android 学习手札(三) 视图(View)

    在Android 系统红,任何可视化组件都需要从android.view.View类继承.可以使用两种方式创建View对象. · 一种方式是使用XML来配置View的相关属性,然后使用相应的方法来装载 ...

  7. 问题:Bringing up interface eth0: Device eth0 does not seem to be present,delaying initialization. [FAILED]—— 找不到网卡。

    克隆虚拟机的时候或其他情况出现以下问题(命令service network restart):   Bringing up interface eth0:  Device eth0 does not ...

  8. 用gtest实现数据驱动的单元测试

    //使用gtest进行数据驱动的单元测试 #include <gtest/gtest.h> #include <iostream> #include <vector> ...

  9. uboot的mtd功能支持

    一.概述 1.MTD MTD是Flash的一种管理方法,将Flash划分成几个分区,便于管理. u-boot的MTD功能是在文件cmd_jffs2.c中实现的,由此我们可以知道怎样打开u-boot的M ...

  10. VC6.0中重载操作符函数无法访问类的私有成员

    整理日: 2015年03月18日 在 C++ 中,操作符(运算符)可以被重载以改写其实际操作.同时我们可以定义一个函数为类的朋友函数(friend function)以便使得这个函数能够访问类的私有成 ...