有一说一,我觉得这题没有到困难级

要保存之前的状态,感觉是很明显的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的更多相关文章

  1. [Swift]LeetCode32. 最长有效括号 | Longest Valid Parentheses

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

  2. Leetcode32. 最长有效括号

    32. 最长有效括号 做法 \(f_{i}\)以\(i\)结尾的最长匹配 前提为\(s[i]=')'\) \(s[i-1]='('\),则\(f[i]=f[i-2]+2\) \(s[i-1]=')'\ ...

  3. (CodeForces - 5C)Longest Regular Bracket Sequence(dp+栈)(最长连续括号模板)

    (CodeForces - 5C)Longest Regular Bracket Sequence time limit per test:2 seconds memory limit per tes ...

  4. 九度OJ 1342:寻找最长合法括号序列II (DP)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:898 解决:366 题目描述: 假如给你一个由'('和')'组成的一个随机的括号序列,当然,这个括号序列肯定不能保证是左右括号匹配的,所以给 ...

  5. 九度OJ 1337:寻找最长合法括号序列 (DP)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:839 解决:179 题目描述: 给你一个长度为N的,由'('和')'组成的括号序列,你能找出这个序列中最长的合法括号子序列么?合法括号序列的 ...

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

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

  7. leetcode 最长有效括号

    给定一个只包含 '(' 和 ')' 的字符串,找出最长的包含有效括号的子串的长度. 示例 1: 输入: "(()" 输出: 2 解释: 最长有效括号子串为 "()&quo ...

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

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

  9. 【LeetCode 32】最长有效括号

    题目链接 [题解] 设dp[i]表示以第i个字符结尾的最长有效括号的长度. 显然只要考虑s[i]==')'的情况 则如果s[i-1]=='(',则dp[i] = dp[i-2]+2; 如果s[i-1] ...

随机推荐

  1. linux DRM GPU scheduler 笔记

    内核文档:   Overview   The GPU scheduler provides entities which allow userspace to push jobs into softw ...

  2. window.open()打开新窗口教程

    使用 window 对象的 open() 方法可以打开一个新窗口.用法如下: window.open (URL, name, features, replace) 参数列表如下: URL:可选字符串, ...

  3. Spring依赖注入的方式、类型、Bean的作用域、自动注入、在Spring配置文件中引入属性文件

    1.Spring依赖注入的方式 通过set方法完成依赖注入 通过构造方法完成依赖注入 2.依赖注入的类型 基本数据类型和字符串 使用value属性 如果是指向另一个对象的引入 使用ref属性 User ...

  4. 30分钟带你理解 Raft 算法

    为什么需要 Raft? Raft 是什么? Raft 的目标 前置条件:复制状态机 Raft 基础 Leader 选举(选举安全特性) 日志复制(Leader只附加.日志匹配) 安全 学习资料 使用 ...

  5. 京东热 key 探测框架新版发布,单机 QPS 可达 35 万

    https://mp.weixin.qq.com/s/3URAvUF6zwxeF5Kkc1aWHA 京东热 key 探测框架新版发布,单机 QPS 可达 35 万 原创 Hollis Hollis 2 ...

  6. vue初始化页面闪动问题

    使用vue开发时,在vue初始化之前,由于div是不归vue管的,所以我们写的代码在还没有解析的情况下会容易出现花屏现象,看到类似于{{message}}的字样,虽然一般情况下这个时间很短暂,但是我们 ...

  7. LOJ10090

    题目描述 原题来自:USACO 2005 Dec. Gold FJ 有 n 头奶牛(2<=n<=1000) ,编号为1..n .奶牛们将按照编号顺序排成一列队伍(可能有多头奶牛在同一位置上 ...

  8. noip 注意事项 (个人向)

    目录 非常重要 对拍 空间 极限数据 模数,YES/NO等大小写 个人 考场 神仙 czdzx 说要写,我也来写 非常重要 对拍 空间 极限数据 模数,YES/NO等大小写 个人 养身体,不要紧张,不 ...

  9. cookie机制、session机制

    会话(Session)跟踪是Web程序中常用的技术,用来跟踪用户的整个会话.常用的会话跟踪技术是Cookie与Session.Cookie通过在客户端记录信息确定用户身份,Session通过在服务器端 ...

  10. ajax 用fom提交

    $.ajax({ type : "POST", url : "${ctx}/credit/LoanauditCtrl/qwe.do?hetong="+heton ...