【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 个人 ...
随机推荐
- 使用pygal 做chart图的经验分享
看到小芮介绍了pygal文章后, http://rfyiamcool.blog.51cto.com/1030776/1378400, 我一直搞数据工作, 所以对于这种数据的展现很有兴趣. 做了点研究, ...
- EF-Linq将查询结果转换为List<string>
List<int> id_list = new List<int>() { 1 };//测试数据...List<string> guid_list = (from ...
- 串行移位锁存并行输出可级联器件74HC595
一.背景 老同学今天突然咨询关于74HC595,自己没用过,同学说可以级联10级!10级?我艹,这么叼,级联又是 什么鬼,这勾起了我极大兴趣,二话不说,手册down下来研究,并在此做个记录. 二.正文 ...
- hdu.1067.Gap(bfs+hash)
Gap Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
- 【C语言入门教程】4.2 二维数组
C 语言允许使用多维数组,即使用多组小标的数组,二维数组是最常用的多维数组.多维数组在内存中存放数据的顺序与一维数组相同,使用连续的存储单元. 4.2.1 二维数组的一般形式 二维数组的一般声明形式为 ...
- debian8 配置使用 nfs
操作过的步骤: 1.dpkg-reconfigre rpcbind. 2.在终端上退出要挂载的目录. 错误:mount -t nfs 172.16.0.121:/home/junda /mnt,出现以 ...
- java之Date
1.java时间处理 package com.bmkit.util.date; import java.text.DateFormat; import java.text.ParseException ...
- httpd服务访问控制
客户机地址限制 通过配置Order.Deny from.Allow from 来限制客户机 allow.deny :先"允许"后"拒绝" ,默认拒绝所有为明确的 ...
- 02快速学习ExtJs之---第一个HelloWord!
这篇主要讲部署下ExtJS开发环境,以及搭建项目.我们使用ExtJs官方提供的Sencha Cmd来搭建 1.搭建项目 1.下载官方的Sencha Cmd工具,安装. 2..Window用户进入到命令 ...
- maven工程通过命令打包
dos下cd到pom.xml所在的目录,输入maven命令:mvn clean package,回车即可. 会打成一个.war包在target文件夹下.