[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.
题意:找到字符串中,最大的有效括号数
思路:这题是valid parentheses的扩展,也可以利用栈结构来实现。这里我们用栈存放左括号的下标,遇到左括号,将其下标存入栈中。遇到右括号,若此时栈为空,说明这个不是有效括号对里的,跳过,更新有效括号的起始点;若是栈不为空,则栈顶元素出栈。此时,若栈为空,后面不一定没有接合法的有效括号对,所以,计算当前和有效括号起始点的距离,并更新最大值,如:()();若不为空,用当前位置距离栈顶元素的距离和maxlen中的最大值更新maxlen,如:()(()()。参考了Grandyang的博客。代码如下:
class Solution {
public:
int longestValidParentheses(string s)
{
stack<int> stk;
int start=,maxLen=;
for(int i=;i<s.size();++i)
{
if(s[i]=='(')
stk.push(i);
else
{
if(stk.empty())
start=i+;
else
{
stk.pop();
if(stk.empty())
maxLen=max(maxLen,i-start+);
else
maxLen=max(maxLen,i-stk.top());
}
}
}
return maxLen;
}
};
这题还能使用动态规划的方式解:
dp[i]为到i处最长的有效括号,如果s[i]为左括号,则dp[i]为0,因为若字符串是以左括号结束,则不可能为有效的;若是为右括号,有两种情况:
一:其前者s[i-1]为左括号,所以dp[i]=dp[i-2]+2;
二、s[i-1]为右括号且s[i-dp[i-1]-1]为左括号,所以 dp[i] = dp[i-1] + 2 + dp[i-dp[i-1]-2],其中i-dp[i-1]-1对应对应最长括号的起始点
LeetCode OJ代码如下:
class Solution {
public:
int longestValidParentheses(string s)
{
if(s.size()<=) return ;
int maxLen=;
vector<int> dp(s.size(),);
for(int i=;i<s.size();++i)
{
if(s[i]==')'&&i-dp[i-]->=&&s[i-dp[i-]-]=='(')
{
dp[i]=dp[i-]++((i-dp[i-]->=)?dp[i-dp[i-]-]:);
maxLen=max(dp[i],maxLen);
}
}
return maxLen;
}
};
[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] 32. Longest Valid Parentheses 最长有效括号
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- [leetcode]32. 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 (wel ...
- [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: Longest Valid Parentheses分析和实现
题目大意:给出一个只包含字符'('和')'的字符串S,求最长有效括号序列的长度. 很有趣的题目,有助于我们对这种人类自身制定的规则的深入理解,可能我们大多数人都从没有真正理解过怎样一个括号序列是有效的 ...
随机推荐
- 使用union 外加count
explain extended and name='aaa')) t; +----+--------------+------------+-------+---------------+----- ...
- js 去掉下划线,后首个字母变大写
1.驼峰转连字符: var s = "fooStyleCss"; s = s.replace(/([A-Z])/g,"-$1").toLowerCase(); ...
- apache+php开发环境搭建步骤
apache 卸载apache服务命令:sc delete apache 1.在D盘下面新建文件夹php7 2.解压apache到php7文件夹下面 3.修改配置文件 4.安装apache服务C:\w ...
- hdu1969Pie(根据体积二分,分馅饼)
Pie Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...
- 骰子涂色 (Cube painting,UVa 253)
题目描述:算法竞赛入门习题4-4 题目思路:1.旋转其中一个骰子进行匹配 2.进行遍历,如果匹配,就进行相对面的匹配 3.三个对立面都匹配即是一样等价的 //没有按照原题的输入输出 #include ...
- 使用手机登录OWA修改密码的问题
最近发现使用手机端登录OWA,安卓手机是可以修改密码的,如图1,但是iPhone就不成,safari和第三方都不可以,如图二. 图一 图二
- 随机森林random forest及python实现
引言想通过随机森林来获取数据的主要特征 1.理论根据个体学习器的生成方式,目前的集成学习方法大致可分为两大类,即个体学习器之间存在强依赖关系,必须串行生成的序列化方法,以及个体学习器间不存在强依赖关系 ...
- vmware中三种网络连接方式
原文来自http://note.youdao.com/share/web/file.html?id=236896997b6ffbaa8e0d92eacd13abbf&type=note vmw ...
- PK3Err0040
PK3Err0040 The target device is not ready for debugging. Please check your configuration bit setting ...
- Centos7 下nginx nginx-1.13.4 安装
环境:CentOS Linux release 7.3.1611 (Core) Linux localhost.localdomain 3.10.0-514.26.2.el7.x86_64 #1 S ...