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.
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(栈)的更多相关文章
- [LeetCode] 032. Longest Valid Parentheses (Hard) (C++)
指数:[LeetCode] Leetcode 指标解释 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 032. Lon ...
- Java for LeetCode 032 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 ...
- Java [leetcode 32]Longest Valid Parentheses
题目描述: Given a string containing just the characters '(' and ')', find the length of the longest vali ...
- LeetCode 032 Longest Valid Parentheses
题目描述:Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the l ...
- 【leetcode】Longest Valid Parentheses
Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...
- 【leetcode】 Longest Valid Parentheses (hard)★
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 ...
随机推荐
- swiper移动端下不能正常轮播的解决方案-----此坑没躺过估计很难找到正确姿势
<script> var mySwiper = new Swiper('.swiper-container', { direction: 'vertical', //horizontal横 ...
- iTOP-4412开发板-实战教程-ssh服务器移植到arm开发板
本文转自迅为开发板:http://www.topeetboard.com 在前面实战教程中,移植了“串口文件传输工具”,整个移植过程是比较简单的,而且我 们没有做任何协议方面的了解,只是“配置”+“编 ...
- arcpy利用XY创建点
# -*- coding: utf-8 -*-"""Created on Sun Apr 7 15:32:24 2019@author: ""&quo ...
- Webstorm 的 Tab 键调整缩进值
两步即可,注意版本
- css滚动条样式修改
.activeMoreBankList{ height: 188px; overflow-y: auto;} /*滚动条样式*/.activeMoreBankList::-webkit-scrollb ...
- ERROR 1133 (42000): Can't find any matching row in the user table
环境:操作系统:Redhat 7.5 x86-64 数据库版本MySQL 5.7.25 现象:ERROR 1133 (42000): Can't find any matching row in th ...
- HDFS的Java API 对文件的操作
在本次操作中所用到的命令 1.首先启动HDFS $HADOOP_HOME/sbin/start-dfs.sh 2.关防火墙 切换到root用户,执行service iptables stop 3.拷贝 ...
- mybatis中修改了数据,控制台显示成功,数据库没有修改
在mybatis中遇到了修改数据时,控制台显示修改成功,但是去数据库查看并没有修改,这是因为mybatis不时自动提交事务的,所以是不会修改数据库的数据,这是我们加上一句 sqlSession.com ...
- [Python3网络爬虫开发实战] 1.3.4-tesserocr的安装
在爬虫过程中,难免会遇到各种各样的验证码,而大多数验证码还是图形验证码,这时候我们可以直接用OCR来识别. 1. OCR OCR,即Optical Character Recognition,光学字符 ...
- 【memcached】memcached中flags字段的作用
我们一般只注意到memcached的数据结构是key,value,今天看memcached源代码的时候,盯上了flags,没看明白.后来问了一下同事,说PHP当中使用flags标记,标识memcach ...