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.

给定一串只包含左右括号的字符串,判断里面合法的个数。左右括号镶嵌嵌套也是合法的例如(())(),合法的和合法的相连也是合法的。这个用DP是可以的。在http://www.cnblogs.com/huntfor/p/3886111.html中介绍了DP可以通过。在http://www.cnblogs.com/lichen782/p/leetcode_Longest_Valid_Parentheses.html的DP却说通不过。改天要好好复习一下动态规划。

我学习了如下的解法。就是按照从前往后遍历,遇到左括号就放入栈中,栈是用来存左括号的下标的,方便之后计算长度。遇到右括号的话考虑两种情况,如果此时栈为空,那这个右括号就是违法的,用last记录它的位置,这样last就记录了我们遍历到i为止的最后一个违法的数,这样的话,再往下遍历,如果发现栈为空了的时候就可以用i-last来表示刚才在i和last中间合法的个数。如果往下遍历发现栈不为空,那么就用当前的i-栈顶元素,因为栈顶元素记录的是离i最近的一个左括号,所以i减去它就记录了他们之间合法的个数,如果大于max就更新。就这样一直遍历到i结束为止。最后输出max就是结果了

class Solution {
public:
int longestValidParentheses(string s)
{
if (s.size() < )
return ;
stack<int> sta;
int last = -, max = ; for (int i = ; i < s.size(); i++)
{
if (s[i] == '(') // 如果是左括号,就将其对应的下标存到栈中
sta.push(i);
if (s[i] == ')') // 如果是右括号
{
if (sta.empty()) // 如果栈为空,last记录为i,也就是遍历到此i的最后一个不符合的右括号的位置
{
last = i;
}
else // 如果不空,那就pop一个左括号的下标
sta.pop();
}
if (sta.empty()) //如果以上操作,sta为空,那么就记录最后一个右括号到i的个数-1为合法的个数
max = std::max(max, i - last);
else // 如果sta还有左括号,那就用剩下的没有匹配的括号到i的个数-1为合法的个数和max比较
max = std::max(max, i - sta.top());
}
return max;
}
};

leetcode第31题--Longest Valid Parentheses的更多相关文章

  1. leetcode解题报告 32. Longest Valid Parentheses 用stack的解法

    第一道被我AC的hard题!菜鸡难免激动一下,不要鄙视.. Given a string containing just the characters '(' and ')', find the le ...

  2. LeetCode 笔记系列八 Longest Valid Parentheses [lich你又想多了]

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

  3. LeetCode (32) Longest Valid Parentheses

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

  4. 【LeetCode每天一题】Valid Parentheses(有效的括弧)

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  5. leetcode解题报告 32. Longest Valid Parentheses 动态规划DP解

    dp[i]表示以s[i]结尾的完全匹配的最大字符串的长度. dp[] = ; ; 开始递推 s[i] = ')' 的情况 先想到了两种情况: 1.s[i-1] = '(' 相邻匹配 这种情况下,dp ...

  6. LeetCode 32. 最长有效括号(Longest Valid Parentheses) 31

    32. 最长有效括号 32. Longest Valid Parentheses 题目描述 给定一个只包含 '(' 和 ')' 的字符串,找出最长的包含有效括号的子串的长度. 每日一算法2019/6/ ...

  7. 【LeetCode每天一题】Longest Valid Parentheses(最长有效括弧)

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

  8. 【leetcode刷题笔记】Longest Valid Parentheses

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

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

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

随机推荐

  1. mysql大写和小写问题

    曾经做企业项目的时候,用的都是oracle数据库,在新公司项目用的是mysql,有关mysql大写和小写的问题 1   windows下默认mysql是不区分大写和小写的,要想让其支持大写和小写.更改 ...

  2. iOS_21团购_发送请求【点评】数据

    结果表明,一个简单的请求: 用到的点评封装的类: 使用tableView简单展示: // // DealListController.m // 帅哥_团购 // // Created by beyon ...

  3. libsvm工具箱C++编程实践2

    转载请注明出处  http://blog.csdn.net/u013491262/article/details/37344193   点击打开链接 上周因为皮肤有点过敏,去医院来来回回一周. 前几天 ...

  4. MVC 如何在一个同步方法(非async)方法中等待async方法

    MVC 如何在一个同步方法(非async)方法中等待async方法 问题 首先,在ASP.NET MVC 环境下对async返回的Task执行Wait()会导致线程死锁.例: public Actio ...

  5. 左右GNU Linux企业加密文件系统 eCryptfs简介

    /*********************************************************************  * Author  : Samson  * Date   ...

  6. E - Speed Limit(2.1.1)

    E - Speed Limit(2.1.1) Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I ...

  7. 使用nodeitk进行对象识别

    前言 东莞,晴,29至27度.忙了一天,最终能够写写东西了.今天继续昨天的话题,我们在昨天的例了基础上完好,通过匹配关键点求出映射从而找到场景中的已知对象. 目标 本文你将学习 採用nodeitk的f ...

  8. Visual Studio Code开发TypeScript

    [Tool] 使用Visual Studio Code开发TypeScript   [Tool] 使用Visual Studio Code开发TypeScript 注意 依照本篇操作步骤实作,就可以在 ...

  9. ENode框架初始化

    ENode框架初始化 前言 Conference案例是使用ENode框架来开发的.之前我没有介绍过ENode框架是如何启动的,以及启动时要注意的一些点,估计很多人对ENode框架的初始化这一块感觉很复 ...

  10. linux_shell_类似sql的orderby 取最大值

    {} {} {} {} {} {} {} {} {} {} 需求场景 ,通过shell 筛选 每分钟的最大一条输出(最后一条) : .info | head