32. Longest Valid Parentheses (Stack; DP)
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)的更多相关文章
- leetcode解题报告 32. Longest Valid Parentheses 用stack的解法
第一道被我AC的hard题!菜鸡难免激动一下,不要鄙视.. Given a string containing just the characters '(' and ')', find the le ...
- 32. Longest Valid Parentheses(最长括号匹配,hard)
Given a string containing just the characters '(' and ')', find the length of the longest valid (w ...
- LeetCode 32 Longest Valid Parentheses(最长合法的括号组合)
题目链接: https://leetcode.com/problems/longest-valid-parentheses/?tab=Description Problem :已知字符串s,求出其 ...
- [Leetcode][Python]32: Longest Valid Parentheses
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 32: Longest Valid Parentheseshttps://oj ...
- 刷题32. Longest Valid Parentheses
一.题目说明 题目是32. Longest Valid Parentheses,求最大匹配的括号长度.题目的难度是Hard 二.我的做题方法 简单理解了一下,用栈就可以实现.实际上是我考虑简单了,经过 ...
- leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、
20. Valid Parentheses 错误解法: "[])"就会报错,没考虑到出现')'.']'.'}'时,stack为空的情况,这种情况也无法匹配 class Soluti ...
- [LeetCode] 32. Longest Valid Parentheses 最长有效括号
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- Longest Valid Parentheses(最长有效括号)
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- 【LeetCode每天一题】Longest Valid Parentheses(最长有效括弧)
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
随机推荐
- elixir 集成ejabberd
备注: 我开发测试的环境时centos 1. 预备环境 1. openssl yum install -y openssl-devel 2. xml yum install -y expat-dev ...
- 系列文章--一步一步学Silverlight2
概述 由TerryLee编写的<Silverlight 2完美征程>一书,已经上市,在该系列文章的基础上补充了大量的内容,敬请关注.官方网站:http://www.dotneteye.cn ...
- js各种效果
1.JavaScript 仿LightBox内容显示效果 2.固定高度的div,竖直方向出现滚动条,水平方向固定 http://www.jb51.net/css/109928.html <!do ...
- springboot: 使web项目支持jsp
1.springboot为什么不推荐使用jsp? 参考地址:https://spring.io/blog/2012/10/30/spring-mvc-from-jsp-and-tiles-to-thy ...
- FastAdmin env.sample 的用法
FastAdmin env.sample 的用法 在 FastAdmin 的 1.0.0.20180513 中我提交了一个 PR,增加 env.sample 内容如下: [app] debug = f ...
- python编程规范系列--建议01~07
本系列来自<编写高质量代码 改善python程序的91个建议>的读书笔记整理. 本书主要内容 1)容易被忽视的重要概念和常识,如代码的布局和编写函数的原则等: 2)编写py ...
- wpf 虚拟化操作异常
根据这篇文章提供的方法会导致搜索变慢及有时候搜索不到 WPF中ItemsControl应用虚拟化时找到子元素的方法, 具体可以修改为下面代码: //Action action = () => / ...
- git 清除本地无效的分支
远程服务器的分支已经删掉了,但是本地分支还存在 $ git fetch -p 如果不行,使用下面的指令 $ git remote prune origin
- 如何配置Python环境
(1) 下载:请在Python官网下载页面(https://www.python.org/downloads/)选择合适的版本(建议选择3.5.2版)的链接,在该版本的下载页面选择合适的安装文件:64 ...
- linux用户,组,文件等操作
参考: https://blog.csdn.net/chengqiuming/article/details/78601977 , https://www.cnblogs.com/123-/p/4 ...