【问题描写叙述】

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.

1.【基础知识】

压栈、弹栈机制可以非常好的模仿括号配对。

2.【屌丝代码】

  • 完毕失败。
  • 达成思想:

1)字符串截取。取到第一个'('为字符串首元素;

2)字符串的元素小于等于1。合法长度推断为0。

3)依次查找每个正括号为首元素最长合法长度。

  • 卡壳部分

1)未能按'('间隔,迭代实现每个正括号为首元素的最长合法长度。

3.【AC源代码】

// LeetCode, Longest Valid Parenthese
// 使用栈,时间复杂度 O(n)。空间复杂度 O(n)
class Solution {
public:
int longestValidParentheses(string s) {
int max_len = 0, last = -1; // the position of the last ')'
stack<int> lefts; // keep track of the positions of non-matching '('s
for (int i = 0; i < s.size(); ++i) { if (s[i] =='(')
{
lefts.push(i);
}
else
{
if (lefts.empty())
{
// no matching left
last = i;
}
else
{
// find a matching pair
lefts.pop();
if (lefts.empty())
{
max_len = max_len>=i-last?max_len:i-last;
}
else
{
// lefts.top() 表示上一个未匹配的正括号
// i-lefts.top() 表示已匹配括号长度
max_len = max_len>=i-lefts.top()?max_len:i-lefts.top();
}
}
}
}
return max_len;
}
};

4.【复盘】

1)脑子没想好就不要动手写代码,写代码也是信马由缰毫无效率,思路不成熟的上手,在有限时间内,对任务的完毕仅仅会徒增恐惧感与无助感。

2)对于一个没想清楚的问题,去反问一句这是不是关键问题,假设是。就把它想透再下手!

比方本例:最相近的两个符号找到之后,它们是不是一定成对。假设成对接下去是怎么办?之前和之后的操作各自是什么?!坚决将这个问题的求解出来的态度是伟大光荣而正确的。

因此。最先应该思考的不是问题。而是第一个元素怎么開始,进而中间的数值怎么半,边界值会让你关注细节而忽略宏观。

3)由于笔者总有string.find()方法在脑子里面绕,这仅仅是看起来使循环似乎变得简单,仅仅是表面上认为比简单的i++快。

实质上这并不降低计算的时间,往往还会由于方法的调用而费时耗存。

4)为什么这里的成员函数的字符串不是作为常量字符串的?窃以为,将字符串作为參数输入,不如将 const string 作为输入更合理!

5.【算法核心思想】

1)从字符串第一个元素開始,正压栈反弹栈;

2)当次循环未压栈,且栈空。说明出现连续空栈。成对失连;

3)当次循环未压栈,弹栈后栈空,成对暂未失连,则计算当次连续对相对上次失连元素递增数。录入。

4)当次循环未压栈,弹栈后栈非空,成对情况未失连。则计算当次连续对相对栈顶元素递增数。录入。

LeetCode 之 Longest Valid Parentheses(栈)的更多相关文章

  1. [LeetCode] 032. Longest Valid Parentheses (Hard) (C++)

    指数:[LeetCode] Leetcode 指标解释 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 032. Lon ...

  2. Java for LeetCode 032 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 32. Longest Valid Parentheses

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

  5. Java [leetcode 32]Longest Valid Parentheses

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

  6. LeetCode 032 Longest Valid Parentheses

    题目描述:Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the l ...

  7. 【leetcode】Longest Valid Parentheses

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

  8. 【leetcode】 Longest Valid Parentheses (hard)★

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

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

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

随机推荐

  1. linux 10201 ASM RAC 安装+升级到10205

    准备环境的时 ,要4个对外IP,2个对内IP 不超过2T,,一般都用OCFS 高端存储适合用ASM linux10G安装的时候,安装的机器时间要小于等于(如果是等于要严格等于)第二个机器的时间(只有l ...

  2. reading from files

    如果有图会很好理解,最近太忙,以后再加吧 #首先有一个需要读取的文件名列表 #然后将文件名列表通过函数string_input_producer放进文件名队列. #有时候因为数据量太大,需要把他们放进 ...

  3. 手机信号强度单位:dBm 和 asu

    介绍 首先明确:dBm 和 asu 是两个独立的单位,它们的换算关系不唯一. 在 2G 网络下:dBm = -113+2*asu在 4G 网络下:dBm = -140+asu dBm 和 asu 都用 ...

  4. .less css 使用 LESS 简化层叠样式表(CSS)的编写(另外一种css框架 sass)

    使用 LESS 简化层叠样式表(CSS)的编写 https://less.bootcss.com/ Sass完全兼容所有版本的CSS https://gojs.net/latest/samples/f ...

  5. zabbix4.2学习笔记--监控nginx

    图解一个客户端连接开源版本的Nginx情况 Accepts(接受).Handled(已处理).Requests(请求数)是一直在增加的计数器.Active(活跃).Waiting(等待).Readin ...

  6. hdfs深入:03、hdfs的架构以及副本机制和block块存储

    HDFS分布式文件系统设计目标 1.            硬件错误  由于集群很多时候由数量众多的廉价机组成,使得硬件错误成为常态 2.            数据流访问  所有应用以流的方式访问数 ...

  7. http返回状态码错误

    415 数据格式不正确 415 Unsupported Media Type 服务器无法处理请求附带的媒体格式 后台用json接收 1.将表单数据转换成json数据 2.设置contentType:& ...

  8. Nginx基础篇(2)- Nginx基本配置文件和变量详解

    Nginx基本配置文件和变量详解 1. 基本配置文件 /etc/nginx/nginx.conf # nginx运行的用户 user nginx; # nginx进程数,建议设置为等于CPU总核心数. ...

  9. Apache Ambari 2.7.3.0 离线安装

    1. 准备 (内存 3G 硬盘 40G) 0)设置ssh无密码 ssh-keygencat id_rsa.pub >> authorized_keyschmod 700 ~/.sshchm ...

  10. Leetcode 143.重排链表

    重排链表 给定一个单链表 L:L0→L1→…→Ln-1→Ln ,将其重新排列后变为: L0→Ln→L1→Ln-1→L2→Ln-2→… 你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换. 示 ...