[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,求最长有效括号序列的长度. 很有趣的题目,有助于我们对这种人类自身制定的规则的深入理解,可能我们大多数人都从没有真正理解过怎样一个括号序列是有效的 ...
随机推荐
- 说说NSCache优于NSDictionary的几点
1.NSCache可以提供自动删减缓存功能,而且保证线程安全,与字典不同,不会拷贝键.2.NSCache可以设置缓存上限,限制对象个数和总缓存开销.定义了删除缓存对象的时机.这个机制只对NSCache ...
- mysql中的直方图采样逻辑
int handler::sample_next(uchar *buf) { // Temporary set inited to RND, since we are calling rnd_next ...
- c++ reference can not be reassigned
#include <iostream> using namespace std; int main () { // declare simple variables int i; int ...
- 「日常训练」ZgukistringZ(Codeforces Round #307 Div. 2 B)
题意与分析(CodeForces 551B) 这他妈哪里是日常训练,这是日常弟中弟. 题意是这样的,给出一个字符串A,再给出两个字符串B,C,求A中任意量字符交换后(不限制次数)能够得到的使B,C作为 ...
- HTML随笔3
1. *svg(可伸缩矢量图)标签画圆,其中r表示半径,cx和cy表示其圆心的坐标 <svg><circle r="100" cx="200" ...
- Python列表的深拷贝和浅拷贝
1. Python列表的拷贝 对于python里面如果想要进行列表的拷贝和复制,具体的操作语句如下: 1) 深拷贝: M=[A,b,a,c] N=M[:] 2) 浅拷贝: N=M 有人说可以直接将M赋 ...
- 【转】从零开始学习Skynet_examples研究
转自 http://blog.csdn.net/mr_virus/article/details/52330193 一.编译Skynet: 1.用ubuntu15.10直接 make linux 编译 ...
- Dask教程
Dask 介绍 Dask是一款用于分析计算的灵活并行计算库. Dask由两部分组成: 针对计算优化的动态任务调度.这与Airflow,Luigi,Celery或Make类似,但针对交互式计算工作负载进 ...
- ubuntu ssh配置
Secure Shell (SSH) is a cryptographic network protocol for operating network services securely over ...
- Using APIs in Your Ethereum Smart Contract with Oraclize
Homepage Coinmonks HOMEFILTER ▼BLOCKCHAIN TUTORIALSCRYPTO ECONOMYTOP READSCONTRIBUTEFORUM & JOBS ...