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. int longestValidParentheses(string s) {
  2. int maxLen = ;
  3. int lastError = -;
  4. vector<int> stack;
  5. for(int i=; i<s.size(); i++)
  6. {
  7. if (s[i] == '(')
  8. stack.push_back(i);
  9. else if (s[i] == ')')
  10. {
  11. if (stack.size()> )
  12. {
  13. stack.pop_back();
  14. int len;
  15. if (stack.size()==)
  16. len = i - lastError;
  17. else
  18. len = i - stack.back();
  19. if (len > maxLen)
  20. maxLen = len;
  21. }
  22. else
  23. lastError = i;
  24. }
  25. }
  26. return maxLen;
  27. }

32. Longest Valid Parentheses *HARD*的更多相关文章

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

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

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

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

  3. 刷题32. Longest Valid Parentheses

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

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

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

  5. leetcode 32. Longest Valid Parentheses

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

  6. 32. Longest Valid Parentheses

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

  7. Java [leetcode 32]Longest Valid Parentheses

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

  8. leetcode problem 32 -- Longest Valid Parentheses

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

  9. 【Python】32. Longest Valid Parentheses

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

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

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

随机推荐

  1. PHP实现多进程并行操作,可做守护进程(转,备用)

    <?php /** * 入口函数 * 将此文件保存为 ProcessOpera.php * 在terminal中运行 /usr/local/php/bin/php ProcessOpera.ph ...

  2. 02: tornado进阶篇

    目录:Tornado其他篇 01: tornado基础篇 02: tornado进阶篇 03: 自定义异步非阻塞tornado框架 04: 打开tornado源码剖析处理过程 目录: 1.1 自定制t ...

  3. 05:ModelForm 数据验证 & 生成html & 数据库操作

    目录:Django其他篇 01:Django基础篇 02:Django进阶篇 03:Django数据库操作--->Model 04: Form 验证用户数据 & 生成html 05:Mo ...

  4. 立几个flag

    有时候会心血来潮想学一点东西,然后搞别的东西的时候就慢慢忘了.. 这里做个备忘录: 树分块/树上莫队 广义后缀自动机(大概这辈子都不会去学了) 带花树(如果我能学的动那个线代的随机算法就放弃这个) 模 ...

  5. C++类的静态成员变量初始化 Win32 API 定时器使用

    1.类的静态成员变量 .h 类声明入下 class A { public: static int x; }; .cpp文件 这样初始化. ; 2.定时器使用 1.SetTimer(HWND,UINT, ...

  6. 《EMCAScript6入门》读书笔记——16.Generator函数的语法

    鼠标指针移到图片上,右键,选择在“在新标签页中打开”,放大即可看到清晰文字.

  7. 项目梳理4——WebApi项目,使用注释填充Description字段

    web.config中添加连接字符串: 为webapi添加Description,使用注释来填充此字段 对于所有引用的xxxx.base项目设置生成的xml文档,注意release.debug下都需设 ...

  8. 【Android实验】组件通信Intent

    实验目的 [TOC] 了解使用Intent进行组件通信原理 掌握使用Intent启动Activity的方法 熟悉和掌握Android组件间通信的方式和技巧 实验要求 设计一个主Activity和一个子 ...

  9. UVa 10905 孩子们的游戏

    https://vjudge.net/problem/UVA-10905 题意: 给定n个正整数,把它们连接成一个最大的整数. 思路: 实在是没想到直接用string来排序了. #include< ...

  10. phpMyAdmin-Error:The mbstring extension is missing. Please check your PHP configuration.

    1.打开php.ini 2.找到 ; extension_dir = "./",把前面的分号去掉.引号里改成extension_dir = "D:php/ext" ...