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 最长的有效括号的更多相关文章

  1. [LeetCode] Longest Valid Parentheses 最长有效括号

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  2. [LeetCode] 32. Longest Valid Parentheses 最长有效括号

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  3. [leetcode]32. Longest Valid Parentheses最长合法括号子串

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  4. [LeetCode] Longest Valid Parentheses 解题思路

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  5. [LeetCode] Longest Valid Parentheses 动态规划

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  6. [LeetCode] Longest Valid Parentheses

    第一种方法,用栈实现,最容易想到,也比较容易实现,每次碰到‘)’时update max_len,由于要保存之前的‘(’的index,所以space complexity 是O(n) // 使用栈,时间 ...

  7. LeetCode: Longest Valid Parentheses 解题报告

    Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...

  8. [LeetCode] Longest Valid Parentheses -- 挂动态规划羊头卖stack的狗肉

    (Version 1.3) 这题在LeetCode上的标签比较有欺骗性,虽然标签写着有DP,但是实际上根本不需要使用动态规划,相反的,使用动态规划反而会在LeetCode OJ上面超时.这题正确的做法 ...

  9. leetcode: Longest Valid Parentheses分析和实现

    题目大意:给出一个只包含字符'('和')'的字符串S,求最长有效括号序列的长度. 很有趣的题目,有助于我们对这种人类自身制定的规则的深入理解,可能我们大多数人都从没有真正理解过怎样一个括号序列是有效的 ...

随机推荐

  1. ACID、数据库隔离级别

    ACID: A(Atomicity):原子性,要么全部执行,要么都不执行 C(consistency):一致性: 特点: 1.一个操作除法级联,这些必须成功,否则全部失败(原子性) 2.所有节点同步更 ...

  2. Python字符串操作大全(非常全!!!)

    1. python编程里字符串的内置方法(非常全) capitalize() 把字符串的第一个字符改为大写 casefold() 把整个字符串的所有字符改为小写 center(width) 将字符串居 ...

  3. 【转】APP推广什么是cpa,cps,cpm

    转载自:http://www.apptg.cn 经常做做APP推广和做运营的同学对于cpa,cps,cpm,cpc这些名词肯定不会陌生,也基本都知道其表示的含义,但是对于新手来说,这几个词的含义还是不 ...

  4. C if 判断 else 否则

    #include <stdio.h> int main(int argc, char **argv) { //新建三个变量进行比较 int a,b,c; //输入三个变量的值scanf(& ...

  5. TW实习日记:第16天

    前端的样式bug实在是太太太莫名其妙了,尤其是封装好的组件,一层套一层的,根本不知道是哪一层出了问题...除了改bug就是做新功能,真想吐槽一下这个项目的留言板,根本没人会用吧...这功能实在是太老旧 ...

  6. Stunnel客户端安装和配置

    声明:本文并非原创,转自华为云帮助中心的分布式缓存服务(DCS)的用户指南. 本文以公网访问华为云分布式缓存服务的Redis缓存为示例,介绍Stunnel客户端在各操作系统下的安装和配置. Stunn ...

  7. Python数据分析实战-Boston Public Schools GEO数据分析-Part1

    项目目标: Boston Public Schools Geo数据是来自于Boston地区的公共学校的数据,具体描述了学校的坐标,名字,类型等.基于此数据,我们可以学习一些基本的Python数据分析的 ...

  8. NMAP-高级用法

    1.报文分段 2.偏移 –mtu后面的数字是8的倍数 3.源端口欺骗 4.指定报文长度 5.ttl 6.mac地址伪造 0代表随机伪造 7.正常输出 8.输出为xml 9.输出为grep 10.输出所 ...

  9. C语言实验——时间间隔

    Description 从键盘输入两个时间点(24小时制),输出两个时间点之间的时间间隔,时间间隔用“小时:分钟:秒”表示. 如:3点5分25秒应表示为--03:05:25.假设两个时间在同一天内,时 ...

  10. mysql入门 — (1)

    使用cd进入到mysql/bin文件夹下面,或者配置完环境之后,直接在cmd中使用mysql,然后回车开启mysql. 登录 为了安全考虑,在这里只设置了本地root用户可以连接上数据库.使用的指令是 ...