[LeetCode] Longest Valid Parentheses 动态规划
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.
#include <string>
#include <iostream>
#include <vector>
using namespace std; class Solution {
public:
int longestValidParentheses(string s) {
int len = s.length();
if(len<) return ;
vector<int > table(len+,);
int cnt = ;
if(s[]=='(') cnt++;
else cnt --;
int retMax = ;
for(int i=;i<len;i++){
if(s[i]=='('){
if(cnt<) cnt=;
else cnt++;
continue;
}
cnt--;
if(cnt>=){
if(s[i-]=='(') table[i+] = table[i-]+;
else{
if(s[i--table[i]]=='(')
table[i+] = table[i--table[i]]++table[i];
}
if(retMax<table[i+]) retMax = table[i+];
}
}
return retMax;
}
}; int main()
{
Solution sol;
cout<<sol.longestValidParentheses("()(())")<<endl;
return ;
}
[LeetCode] Longest Valid Parentheses 动态规划的更多相关文章
- [LeetCode] 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 (well-f ...
- [LeetCode] Longest Valid Parentheses 解题思路
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- [LeetCode] Longest Valid Parentheses
第一种方法,用栈实现,最容易想到,也比较容易实现,每次碰到‘)’时update max_len,由于要保存之前的‘(’的index,所以space complexity 是O(n) // 使用栈,时间 ...
- LeetCode: Longest Valid Parentheses 解题报告
Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...
- [LeetCode] Longest Valid Parentheses -- 挂动态规划羊头卖stack的狗肉
(Version 1.3) 这题在LeetCode上的标签比较有欺骗性,虽然标签写着有DP,但是实际上根本不需要使用动态规划,相反的,使用动态规划反而会在LeetCode OJ上面超时.这题正确的做法 ...
- leetcode解题报告 32. Longest Valid Parentheses 动态规划DP解
dp[i]表示以s[i]结尾的完全匹配的最大字符串的长度. dp[] = ; ; 开始递推 s[i] = ')' 的情况 先想到了两种情况: 1.s[i-1] = '(' 相邻匹配 这种情况下,dp ...
- leetcode: Longest Valid Parentheses分析和实现
题目大意:给出一个只包含字符'('和')'的字符串S,求最长有效括号序列的长度. 很有趣的题目,有助于我们对这种人类自身制定的规则的深入理解,可能我们大多数人都从没有真正理解过怎样一个括号序列是有效的 ...
- leetcode Longest Valid Parentheses python
class Solution(object): def longestValidParentheses(self, s): """ :type s: str :rtype ...
随机推荐
- 浅谈压缩感知(二十八):压缩感知重构算法之广义正交匹配追踪(gOMP)
主要内容: gOMP的算法流程 gOMP的MATLAB实现 一维信号的实验与结果 稀疏度K与重构成功概率关系的实验与结果 一.gOMP的算法流程 广义正交匹配追踪(Generalized OMP, g ...
- 文件上传小技巧/后端处理【以php示例】
引语:在上一篇文章中说到,在页面中可以用隐藏的方式让你的上传页面看起来漂亮.但是这对于性能来说,并没有什么卵用,那么在后台的处理中,难道就没有一些处理技巧么?所谓后台的技巧,应该要包括上传得快一点,上 ...
- iOS开发-状态模式
状态模式允许对象内部状态改变时改变它的行为,对象看起来好像修改了它的类.状态模式看起来和策略模式比较相像,策略模式是将可以互换的行为封装起来,然后通过使用委托的方式,决定使用哪一个行为,状态也是封装行 ...
- asp.net对cookie的操作
创建cookie: HttpCookie cookie = new HttpCookie("CurrentUser"); //创建一个名称为CurrentUser 的cookie对 ...
- Comet技术详解:基于HTTP长连接的Web端实时通信技术
前言 一般来说,Web端即时通讯技术因受限于浏览器的设计限制,一直以来实现起来并不容易,主流的Web端即时通讯方案大致有4种:传统Ajax短轮询.Comet技术.WebSocket技术.SSE(Ser ...
- 关于iReport报表的分页
问题:二手车认证系统的检测报告采用iReport开发,开发者自定义了一张超级长的纸张,导致打印时自动缩放到了一张A4纸上.需要修改使之能够合理的分页打印,这是来到新公司的第一个任务. 解决方案一: 1 ...
- php类自动装载、链式操作、魔术方法
1.自动装载实例 目录下有3个文件:index.php load.php tests文件夹 tests文件夹里有 test1.php <?php namespace Tests; class T ...
- android: 播放音频
在 Android 中播放音频文件一般都是使用 MediaPlayer 类来实现的,它对多种格式的音 频文件提供了非常全面的控制方法,从而使得播放音乐的工作变得十分简单.下表列出了 MediaPlay ...
- 利用同一 ASP.NET 的多个代码框架
2012 年,Microsoft 推出了两个添加到 ASP.NET 工具包的新框架:Web API 和 SignalR. 这两个框架为开发环境带来独特的开发方式,每个框架都有自身的独特之处: Web ...
- maven继承parent,relativePath warn信息的解决办法
往下看之前一定要先看 %MAVEN_HOME%/conf/settings.xml 配置文件的是否更改了,是否配置正确 <mirror> <id>nexus</id> ...