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. FastAdmin 线上部署流程 (2018-05-03 更新)

    FastAdmin 线上部署流程 首次部署 建立 git 环境. 建立 composer 环境. 建立 bower 环境. 将远程项目代码 git clone 到服务器上. 执行 composer i ...

  2. (转)Inno Setup入门(二十二)——Inno Setup类参考(8)

    本文转载自:http://blog.csdn.net/yushanddddfenghailin/article/details/17268473 列表框 列表框(ListBox)是Windows应用程 ...

  3. java web 程序---登陆验证4个页面

    思路: 1.第一个是登陆页面login.jsp一个form表单.点击登陆按钮 2.第二个是验证页面check.jsp.如果username和password都正确.则跳转到另一个页面a.jsp显示登陆 ...

  4. Hive使用druid做连接池代码实现

    配置文档 hive_jdbc_url=jdbc:hive2://192.168.0.22:10000/default hive.dbname=xxxxx hive_jdbc_username=root ...

  5. Nginx启停

    启动nginx /usr/local/nginx/nginx #不指定配置文件地址/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx. ...

  6. 用户的 添加 权限 MySql远程登录

    添加一个用户 '; 为这个叫mongo的用户赋予操作z_0811数据库的所有权限 '; mysql如何修改开启允许远程连接   关于mysql远程连接的问题,大家在公司工作中,经常会遇到mysql数据 ...

  7. SSH框架的简化(struts2、spring4、hibernate5)

    目的: 通过对ssh框架有了基础性的学习,本文主要是使用注解的方式来简化ssh框架的代码编写. 注意事项: 1.本文提纲:本文通过一个新闻管理系统的实例来简化ssh框架的代码编写,功能包括查询数据库中 ...

  8. REST理解

    内容摘自:<Spring REST> REST是什么:REST是一种软件架构风格,它由建立规模可扩展的web服务的最佳实践和指南构成. 资源: 一切可被访问和操作的东西.资源标识:URI( ...

  9. 用嵌套List实现DataGrid的主从表显示

    //首先构造嵌套List,也就是一个list在另一个list中充当成员//如:referModels 在res中充当成员var res = totalAffectedMedels.Select(c = ...

  10. 异步编程之Generator(2)——剖析特性

    异步编程系列教程: (翻译)异步编程之Promise(1)--初见魅力 异步编程之Promise(2):探究原理 异步编程之Promise(3):拓展进阶 异步编程之Generator(1)--领略魅 ...