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. Navicat Premium 连接O​r​a​c​l​e​ ​出现o​r​a​-​1​2​5​0​5​ ​错​误​解​决​方​案

    找到listener.ora文件:我的Oracle是安装在F盘, 路径为:F:\oracle\product\10.2.0\db_1\network\admin\listener.ora  改动前的配 ...

  2. linux tcpdump命令抓包

    tcpdump host 210.27.48.1 and \ (210.27.48.2 or 210.27.48.3 \) 截获主机210.27.48.1 和主机210.27.48.2 或210.27 ...

  3. angular-ui-bootstrap的弹出框定义成一个服务的实践(二)

    定义一个弹出框的服务:alert_box defiine(["app"],function(mainapp){ mainapp.controller('ModalInstanceC ...

  4. Eclipse Failed to load D:\android-sdk-windows\build-tools\27.0.3\lib\dx.jar

    Failed to load D:\android-sdk-windows\build-tools\27.0.3\lib\dx.jar Unknown error: Unable to build: ...

  5. rapidjson的read和write的sample

    头文件 #include "json/document.h" #include "json/prettywriter.h" #include "jso ...

  6. 2DAY初识python

    一.变量 1 什么是变量之声明变量 #变量名=变量值 age=18 gender1='male' gender2='female' 2 为什么要有变量 变量作用:“变”=>变化,“量”=> ...

  7. Java中的<< 和 >> 和 >>> 详细分析

    <<表示左移移,不分正负数,低位补0: 注:以下数据类型默认为byte-8位 左移时不管正负,低位补0 正数:r = 20 << 2 20的二进制补码:0001 0100 向左 ...

  8. Java 版本6下载大全

    Oracle 官方 JDK6 下载地址: 基本包含所有的JDK6版本. 需要登注册相应的账户登录到Oracle官网~ http://www.oracle.com/technetwork/java/ja ...

  9. ApacheOFBiz的相关介绍以及使用总结(三)

    Ofbiz中还提供了一些基础性服务,可以直接用来使用,下面就简单介绍说明一下.   ofbiz邮件发送服务   ofbiz中提供发送邮件相关功能:sendMailFromScreen   contex ...

  10. python学习 (三十三) Modules

    1: 方法一: 导入整个模块 import math class ModulesDemo(): def builtin_modules(self): print(math.sqrt()) m = Mo ...