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.

int longestValidParentheses(string s) {
int maxLen = ;
int lastError = -;
vector<int> stack;
for(int i=; i<s.size(); i++)
{
if (s[i] == '(')
stack.push_back(i);
else if (s[i] == ')')
{
if (stack.size()> )
{
stack.pop_back();
int len;
if (stack.size()==)
len = i - lastError;
else
len = i - stack.back();
if (len > maxLen)
maxLen = len;
}
else
lastError = i;
}
}
return maxLen;
}

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. FreeModbus移植实例(转)

    源:分享FreeRTOS + FreeModbus + UART_RTO FREERTOS 移植学习 入门必备 正点原子官方所有开发板的FreeRTOS教程及其例程正式发布(STM32F103,STM ...

  2. 原生JS操作AJAX

    1,get方式的AJAX function sendAjaxReq() { //1,创建ajax引擎 XMLHttpRequest对象 var req = new XMLHttpRequest() | ...

  3. 第几天|2018年蓝桥杯B组题解析第一题-fishers

    标题:第几天 2000年的1月1日,是那一年的第1天. 那么,2000年的5月4日,是那一年的第几天? 注意:需要提交的是一个整数,不要填写任何多余内容. 思路:计算日期,用excel计算两个日期的差 ...

  4. 源码编译安装keepalived

    首先到官网下载需要的包:http://www.keepalived.org/download.html [root@localhost local]# .tar.gz [root@localhost ...

  5. 将DevExpress.Utils.ImageCollection变量的image导出

    private void tspBtnExportExcel_Click(object sender, EventArgs e) { //暂时用来导出图片 string filePath = Syst ...

  6. 帮助大家理解java中的随机和继承,动态绑定.

    package com.ykmimi.javabianchengsixiang; /** * 形状的继承 随机形状生成器 * @author ukyor */ import java.util.Ran ...

  7. python如何安装第三方库

    1.python集成开发环境pycharm如何安装第三方库 http://blog.csdn.net/qiannianguji01/article/details/50397046 有的时候安装不上第 ...

  8. 用python + hadoop streaming 编写分布式程序(二) -- 在集群上运行与监控

    写在前面 相关随笔: Hadoop-1.0.4集群搭建笔记 用python + hadoop streaming 编写分布式程序(一) -- 原理介绍,样例程序与本地调试 用python + hado ...

  9. js ajax跨域

    一般情况后台返回... 也就是说,无论数据本身是什么数据类型,数据,对象,都是以字符串形式返回的. 如何把字符串化成相应对象. 如: var s='{"left":100}' co ...

  10. 数据库与hadoop与分布式文件系统的区别和联系

    转载一篇关系数据库与Hadoop的关系的文章 1. 用向外扩展代替向上扩展 扩展商用关系型数据库的代价是非常昂贵的.它们的设计更容易向上扩展.要运行一个更大的数据库,就需要买一个更大的机器.事实上,往 ...