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.

法I:把所有invalid的括号位置都标记出来,比较invalid之间的长度哪段最长

class Solution {
public:
int longestValidParentheses(string s) {
vector<int> invalidPos;
invalidPos.push_back(-);
invalidPos.push_back(s.length());
stack<int> lParenPos;
int len = , ret = ; for(int i = ; i < s.length(); i++){
if(s[i]=='('){
lParenPos.push(i);
}
else{ //right parenthese
if(lParenPos.empty()){
invalidPos.push_back(i);
}
else{
lParenPos.pop();
}
}
} while(!lParenPos.empty()){
invalidPos.push_back(lParenPos.top());
lParenPos.pop();
} sort(invalidPos.begin(), invalidPos.end());
for(int i = ; i < invalidPos.size(); i++){
len = invalidPos[i]-invalidPos[i-]-;
if(len > ret) ret = len;
} return ret;
}
};

法II:动态规划

class Solution {
public:
int longestValidParentheses(string s) {
if(s.empty()) return ;
stack<int> leftStack;
int ret = ;
int currentMax = ;
int leftPos;
vector<int> dp(s.length()+,); //currentMax无法检测到连续valid的情况,eg: ()(), 所以需要动态规划记录i位置之前连续多少个valid。 for(int i = ; i <s.length(); i++){
if(s[i]==')'){
if(leftStack.empty()){
currentMax = ;
}
else
{
leftPos = leftStack.top();
leftStack.pop();
currentMax = i-leftPos+ + dp[leftPos];
dp[i+] = currentMax;
ret = max(ret,currentMax);
}
}
else{
leftStack.push(i); //push the index of '('
}
}
return ret;
}
};

32. Longest Valid Parentheses (Stack; DP)的更多相关文章

  1. leetcode解题报告 32. Longest Valid Parentheses 用stack的解法

    第一道被我AC的hard题!菜鸡难免激动一下,不要鄙视.. Given a string containing just the characters '(' and ')', find the le ...

  2. 32. Longest Valid Parentheses(最长括号匹配,hard)

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

  3. LeetCode 32 Longest Valid Parentheses(最长合法的括号组合)

    题目链接: https://leetcode.com/problems/longest-valid-parentheses/?tab=Description   Problem :已知字符串s,求出其 ...

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

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

  5. 刷题32. Longest Valid Parentheses

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

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

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

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

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

  8. Longest Valid Parentheses(最长有效括号)

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

  9. 【LeetCode每天一题】Longest Valid Parentheses(最长有效括弧)

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

随机推荐

  1. Docker-Compose API too old for Windows

    I was working on some code with a Docker Windows container today and ran into this error message: ER ...

  2. webpack中imports-loader,exports-loader,expose-loader的区别

    Webpack有几个和模块化相关的loader,imports-loader,exports-loader,expose-loader,比较容易混淆.今天,我们来理一理. imports-loader ...

  3. (转)Inno Setup入门(四)——为程序创建桌面快捷方式

    本文转载自:http://blog.csdn.net/augusdi/article/details/8564810 Icons这一可选段定义所有创建在开始菜单和\或其它位置 (比如桌面) 的快捷方式 ...

  4. 详细讲解删除SQL Server日志的具体方法

    一: 删除LOG 1:分离数据库 企业管理器->服务器->数据库->右键->分离数据库 2:删除LOG文件 3:附加数据库 企业管理器->服务器->数据库-> ...

  5. java代码----substring()方法是按索引截取字符串。。。下标0开始

    总结:按照索引substring(2,5);意思是从字符串的索引为2开始(包括)到第6个字符(不包括)的位置的截取部分 package com.s.x; //substring public clas ...

  6. 无线加密的多种方法及其区别(WEP WPA TKIP EAP)

    无线加密的多种方法及其区别(WEP WPA TKIP EAP) 无线网络的安全性由认证和加密来保证. 认证允许只有被许可的用户才能连接到无线网络: 加密的目的是提供数据的保密性和完整性(数据在传输过程 ...

  7. SQLServer数据库优化常用语句

    -- 平均物理读次数最多的SQL语句:select top 50 *, (s.total_physical_reads / s.execution_count) as avephysicalreads ...

  8. python学习笔记(十四): unittest

    Python中有一个自带的单元测试框架是unittest模块,用它来做单元测试,它里面封装好了一些校验返回的结果方法和一些用例执行前的初始化操作. 在说unittest之前,先说几个概念: TestC ...

  9. LinkedHashMap学习

    一.概述 LinkedHashMap继承自HashMap,是Map接口的一个具体实现,它是有序的,可以按照插入顺序先后和访问时间先后进行排序,选择哪种排序方式取决于在新建LinkedHashMap的时 ...

  10. Python Twisted系列教程19:改变之前的想法

    作者:dave@http://krondo.com/i-thought-i-wanted-it-but-i-changed-my-mind/  译者: Cheng Luo 你可以从”第一部分 Twis ...