【leetcode】 Longest Valid Parentheses (hard)★
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.
思路:自己按照早上Scramble String (hard)★ 的动态规划方法试着做了一遍,‘(’表示为1, ‘)’表示为-1,用dp[start]记录不同长度情况下以start开始的括号对应的数字之和。如果为0则表示有效,更新最大长度。 结果超时了,说明我这个O(n2)的方法不行。
int longestValidParentheses(string s) {
string scpy = s;
int ans = ;
while(!scpy.empty() && scpy[] == ')') //跳过位于前面的 ) 因为肯定无法配对
{
scpy.erase(scpy.begin());
}
if(scpy.empty())
return ans; vector<int> dp; //dp[start]
vector<int> dp1;
for(int start = ; start <= scpy.length() - ; start++)
{
dp.push_back((scpy[start] == '(') ? : -);
dp1.push_back(dp.back());
}
for(int len = ; len <= scpy.length(); len++)
{
for(int start = ; start <=scpy.length() - len; start++)
{
dp[start] = dp[start] + dp1[start + len - ];
if(dp[start] == && len > ans)
{
ans = len;
}
}
}
return ans;
}
大神可以通过而且简洁的O(n)方法
用longest[i]存储以 i 结尾时最大有效长度(必须包括第 i 个字符)
如果s[i] = '(' longest[i] = 0
else s[i] = ')'
If s[i-1] is '(', longest[i] = longest[i-2] + 2
Else if s[i-1] is ')' and s[i-longest[i-1]-1] == '(', longest[i] = longest[i-1] + 2 + longest[i-longest[i-1]-2]
the condition "s[i-1] == '('" since "s[i-longest[i-1]-1] == '('" actually concludes this case. We could just use "if (i-1>=0 && i-longest[i-1]-1 >=0 && s[i-longest[i-1]-1] == '(')"
int longestValidParentheses(string s) {
if(s.length() <= ) return ;
int curMax = ;
vector<int> longest(s.size(),);
for(int i=; i < s.length(); i++){
if(s[i] == ')' && i-longest[i-]- >= && s[i-longest[i-]-] == '('){
longest[i] = longest[i-] + + ((i-longest[i-]- >= )?longest[i-longest[i-]-]:);
curMax = max(longest[i],curMax);
}
}
return curMax;
}
【leetcode】 Longest Valid Parentheses (hard)★的更多相关文章
- 【leetcode】Longest Valid Parentheses
Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...
- 【LeetCode】20. Valid Parentheses 有效的括号
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:有效,括号,括号匹配,栈,题解,leetcode, 力扣 ...
- 【LeetCode】020. Valid Parentheses
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- 【LeetCode】20. Valid Parentheses
题目:
- [LeetCode] 032. Longest Valid Parentheses (Hard) (C++)
指数:[LeetCode] Leetcode 指标解释 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 032. Lon ...
- 【LeetCode】Longest Word in Dictionary through Deleting 解题报告
[LeetCode]Longest Word in Dictionary through Deleting 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode. ...
- 【LeetCode】36. Valid Sudoku 解题报告(Python)
[LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...
- 【LeetCode】593. Valid Square 解题报告(Python)
[LeetCode]593. Valid Square 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...
- 【LeetCode】678. Valid Parenthesis String 解题报告(Python)
[LeetCode]678. Valid Parenthesis String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人 ...
随机推荐
- javascript自定义滚动条插件,几行代码的事儿
在实际项目中,经常由于浏览器自带的滚动条样式太戳,而且在各个浏览器中显示不一样,所以我们不得不去实现自定义的滚动条,今天我就用最少的代码实现了一个自定义滚动条,代码量区区只有几十行,使用起来也非常方便 ...
- windows系统添加删除用户命令!
参考:net help usernet help group Net user添加或修改用户帐户或者显示用户帐户信息. 语法net user [UserName [Pa ...
- Linux统计文件行数
语法:wc [选项] 文件… 说明:该命令统计给定文件中的字节数.字数.行数.如果没有给出文件名,则从标准输入读取.wc同时也给出所有指定文件的总统计数.字是由空格字符区分开的最大字符串. 该命令各选 ...
- ajax如何返回多个值
应用场景: 在前端有个ajax请求到后端后,需要返回多个变量的值,在这里使用的是Json格式作为值传递,使用eval函数来解析Json格式. 要传递的值data: var data = " ...
- Ubuntu 14 如何解压 .zip、.rar 文件?
.zip 和 .rar 是Windows下常用的压缩文件,在Ubuntu中如何解压? [解压.zip文件] Ubuntu中貌似已经安装了unzip软件,解压命令如下: unzip ./FileName ...
- Go - 内置函数大全
Package builtin import "builtin" Overview Index Overview ▾ Package builtin provides docume ...
- php 通过curl post发送json数据实例
例1 代码如下 复制代码 $data = array("name" => "Hagrid", "age" => "3 ...
- bootstrap-dropdown
功能:实现点击时下拉框显示 插件:dropdown.js 要点:dropdown功能往往用在导航栏.导航条上,用作标题显示.dropdown与<ul><li>标签搭配用. 以d ...
- IE6支持max-height及min-height解决方法
我们在写CSS的时候,常常会遇到让一个图片或一个布局不能超出设定一定高度范围值,有时也需要设置一个最小高度值,以达到对齐等样式.接下来为大家总结的如何解决IE6不支持max-height和不支持min ...
- cocos2d智能指针 转自:http://blog.csdn.net/nxshow/article/details/44699409
智能指针在C++11的标准中已经存在了, 分别是unique_ptr,shared_ptr,weak_ptr, 其中最常用的应该是share_ptr, 它采用引用计数的方式管理内存, 当引用计数为0的 ...