leetcode32 最长游戏括号 dp
有一说一,我觉得这题没有到困难级
要保存之前的状态,感觉是很明显的dp
思路和题解一样

class Solution {
public:
int longestValidParentheses(string s) {
int len=s.length();
int ret = 0;
int *dp=new int[len];
for(int i=0;i<len;i++)
dp[i]=0;
for (int i = 1; i < s.length(); i++) {
if (s[i] == ')') {
if (s[i - 1] == '(') {
dp[i] = (i >= 2 ? dp[i - 2] : 0) + 2;
}
else if (i - dp[i - 1] > 0 && s[i - dp[i - 1] - 1] == '(') {
dp[i] = dp[i - 1] + ((i - dp[i - 1]-2) >= 0 ? dp[i - dp[i - 1] - 2] : 0) + 2;
}
ret=max(ret,dp[i]);
}
}
return ret;
}
};
然后看了题解,因为只有( H和),只需要用left,right记录即可
扫描两遍的原因是 (() 避免这种情况
class Solution {
public:
int longestValidParentheses(string s) {
int res = 0;
int left = 0;
int mark = 0;
for (int i = 0; i < s.size(); ++i) {
int prev_mark = mark;
mark = max(0, mark + ((s[i] == '(') ? 1 : -1));
if (mark == 0) {
if (prev_mark > 0) {
res = max(i - left + 1, res);
} else {
left = i + 1;
}
}
}
mark = 0;
int right = s.size() - 1;
for (int i = s.size() - 1; i >= 0; --i) {
int prev_mark = mark;
mark = max(0, mark + ((s[i] == ')') ? 1 : -1));
if (mark == 0) {
if (prev_mark > 0) {
res = max(right - i + 1, res);
} else {
right = i - 1;
}
}
}
return res;
}
};
leetcode32 最长游戏括号 dp的更多相关文章
- [Swift]LeetCode32. 最长有效括号 | Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- Leetcode32. 最长有效括号
32. 最长有效括号 做法 \(f_{i}\)以\(i\)结尾的最长匹配 前提为\(s[i]=')'\) \(s[i-1]='('\),则\(f[i]=f[i-2]+2\) \(s[i-1]=')'\ ...
- (CodeForces - 5C)Longest Regular Bracket Sequence(dp+栈)(最长连续括号模板)
(CodeForces - 5C)Longest Regular Bracket Sequence time limit per test:2 seconds memory limit per tes ...
- 九度OJ 1342:寻找最长合法括号序列II (DP)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:898 解决:366 题目描述: 假如给你一个由'('和')'组成的一个随机的括号序列,当然,这个括号序列肯定不能保证是左右括号匹配的,所以给 ...
- 九度OJ 1337:寻找最长合法括号序列 (DP)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:839 解决:179 题目描述: 给你一个长度为N的,由'('和')'组成的括号序列,你能找出这个序列中最长的合法括号子序列么?合法括号序列的 ...
- [LeetCode] Longest Valid Parentheses 最长有效括号
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- leetcode 最长有效括号
给定一个只包含 '(' 和 ')' 的字符串,找出最长的包含有效括号的子串的长度. 示例 1: 输入: "(()" 输出: 2 解释: 最长有效括号子串为 "()&quo ...
- [LeetCode] 32. Longest Valid Parentheses 最长有效括号
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- 【LeetCode 32】最长有效括号
题目链接 [题解] 设dp[i]表示以第i个字符结尾的最长有效括号的长度. 显然只要考虑s[i]==')'的情况 则如果s[i-1]=='(',则dp[i] = dp[i-2]+2; 如果s[i-1] ...
随机推荐
- 与图论的邂逅05:最近公共祖先LCA
什么是LCA? 祖先链 对于一棵树T,若它的根节点是r,对于任意一个树上的节点x,从r走到x的路径是唯一的(显然),那么这条路径上的点都是并且只有这些点是x的祖先.这些点组成的链(或者说路径)就是x的 ...
- Angular学习资料大全和常用语法汇总(让后端程序员轻松上手)
前言: 首先为什么要写这样的一篇文章呢?主要是因为前段时间写过一些关于Angualr的相关实战文章,有些爱学习的小伙伴对这方面比较感兴趣,但是又不知道该怎么入手(因为认识我的大多数小伙伴都是后端的同学 ...
- 大数据系列2:Hdfs的读写操作
在前文大数据系列1:一文初识Hdfs中,我们对Hdfs有了简单的认识. 在本文中,我们将会简单的介绍一下Hdfs文件的读写流程,为后续追踪读写流程的源码做准备. Hdfs 架构 首先来个Hdfs的架构 ...
- 针对Fluent-Bit采集容器日志的补充
hello,之前我写过<一套标准的ASP.NET Core容器化应用日志收集分析方案>,在公司团队.微信公众号.Github上反映良好. 其中配置Fluent-bit使用Forward协议 ...
- 0到1:微信后台系统的演进之路 原创 张文瑞 InfoQ 2016-01-14
0到1:微信后台系统的演进之路 原创 张文瑞 InfoQ 2016-01-14
- Architecture and design 洋葱 中间件 装饰器
Go kit - Frequently asked questions https://gokit.io/faq/ Architecture and design Introduction - Und ...
- SICP 解题集 — SICP 解题集 https://sicp.readthedocs.io/en/latest/
SICP 解题集 - SICP 解题集 https://sicp.readthedocs.io/en/latest/
- 如何在opencv下使用SIFT
SIFT即尺度不变特征变换,是用于图像处理领域的一种描述.这种描述具有尺度不变性,可在图像中检测出关键点,是一种局部特征描述子.SIFT的尺度不变特征变换在图像特征点匹配中十分关键,因为我们从不同角度 ...
- FridaHook框架学习(2)
FridaHook框架学习(2) 前言 学习过程参考https://bbs.pediy.com/thread-227233.htm. 逆向分析 安装并运行例子程序,可以看到这个例子是一个验证注册码的程 ...
- 使用 Shiro,从架构谈起,到框架集成!
使用 Shiro,从架构谈起,到框架集成! 一.架构 1.使用用户的登录信息创建令牌 2.执行登陆动作 3.判断用户 4.两条重要的英文 二.实现Realm 1.缓存机制 2.散列算法与加密算法 3. ...