【问题描写叙述】

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源代码】

  1. // LeetCode, Longest Valid Parenthese
  2. // 使用栈,时间复杂度 O(n)。空间复杂度 O(n)
  3. class Solution {
  4. public:
  5. int longestValidParentheses(string s) {
  6. int max_len = 0, last = -1; // the position of the last ')'
  7. stack<int> lefts; // keep track of the positions of non-matching '('s
  8. for (int i = 0; i < s.size(); ++i) {
  9.  
  10. if (s[i] =='(')
  11. {
  12. lefts.push(i);
  13. }
  14. else
  15. {
  16. if (lefts.empty())
  17. {
  18. // no matching left
  19. last = i;
  20. }
  21. else
  22. {
  23. // find a matching pair
  24. lefts.pop();
  25. if (lefts.empty())
  26. {
  27. max_len = max_len>=i-last?max_len:i-last;
  28. }
  29. else
  30. {
  31. // lefts.top() 表示上一个未匹配的正括号
  32. // i-lefts.top() 表示已匹配括号长度
  33. max_len = max_len>=i-lefts.top()?max_len:i-lefts.top();
  34. }
  35. }
  36. }
  37. }
  38. return max_len;
  39. }
  40. };

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. IOS 面试题系列

    随着iOS平台开发的职位的增加,笔试.面试也越来越有“套路”,这里我总结了一些面试题,多数是Objective-C的基础知识,适合于面试新人,答案是我自己答的,不准确的地方,欢迎指出. 1.   Ob ...

  2. zabbix监控之grafana

    zabbix监控之grafana

  3. 第3节 mapreduce高级:12、mapreduce相关的参数调整

    5.1 多job串联 一个稍复杂点的处理逻辑往往需要多个mapreduce程序串联处理,多job的串联可以借助mapreduce框架的JobControl实现 示例代码: ControlledJob ...

  4. 通过反编译小程序来学习前端:wxappUnpacker

    小程序开发时,会有4种文件:.wxss  .json  .wxs  .wxml. 正式上传到腾讯时,目录会被打包,使用时再发放给客户端. 这个文件包后缀是 .wxapkg.只要手机用过这个小程序,文件 ...

  5. 全国高校绿色计算大赛 预赛第二阶段(Python)

    第1关统计分数的麻烦 class Task: def get_lost_scores(self, scores): s = "" index = [1 for i in range ...

  6. Linux网络配置出现的问题

    网络连接 : 选择桥接模式进入字符界面后,管理员登入后  ifconfig显示eth0和ol,但是不显示静态IP地址,即无inet.地址.广播.掩码 解决方案: 1.使用sudo dhclient e ...

  7. buf.entries()详解

    buf.entries() 返回:{Iterator} 从当前 Buffer 的内容中,创建并返回一个 [index, byte] 形式的迭代器. const buf = Buffer.from('b ...

  8. 77-CCI,Commodity Channel Index,商品通道指标.(2015.7.1)

    CCI,Commodity Channel Index 商品通道指标 Channel Index,商品通道指标.(2015.7.1)" title="77-CCI,Commodit ...

  9. 集训第五周动态规划 H题 回文串统计

    Hrdv is interested in a string,especially the palindrome string.So he wants some palindrome string.A ...

  10. web前端开发——css

    一.css介绍 1.css是什么? Cascading Style Sheets缩写,层叠样式表.样式定义如何显示HTML元素,样式通常又会存在于样式表中. 2.为什么需要css? 使HTML页面变得 ...