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 ...
随机推荐
- Asp.Net 设计模式 之 “工厂方法”即利用 接口 实现的抽象工厂
主要改动部分: /// <summary> /// 6.创建工厂方法模式(抽象工厂:接口) /// </summary> interface IFactory ...
- mysql 插入多条记录,重复值不插入
只去除主键与唯一索引的字段,字段为null时 是可以重复插入的domo: insert ignore into table_name(email,phone,user_id) values('test ...
- ActiveSync日志分析
Here are the steps to exporting the ActiveSync log data: Create a folder on the root of C:\ ...
- Activiti数据库表结构(表详细版)
http://blog.csdn.net/hj7jay/article/details/51302829 1 Activiti数据库表结构 1.1 数据库表名说明 Activiti工作流总 ...
- WPF小记 -- 使用Path自己画图标,点击命中(焦点)丢失问题
在Template中,Path外面的Grid需添加Background属性值.否则点击范围会受限制,例如:Click,在RadioButton的Height和With范围内点击,命中率<1. & ...
- 安装gitlab并配置邮箱
安装要求:运行内存必须大于等于2G 一.安装docker wget -qO- https://get.docker.com/ | sh 镜像加速: echo '{"registry-mirr ...
- c语言 c++ 实现查看本地ip,外网ip, 本地主机名,查看http网址对应的ip
/******************************************************************************* 作者 :邓中强 Email :1246 ...
- vue-router 基本使用(vue工程化)
(1)概念: 路由,其实就是指向的意思,当我点击页面上的home按钮时,页面中就要显示home的内容,如果点击页面上的about 按钮,页面中就要显示about 的内容.Home按钮 => h ...
- JavaSE-11 接口
学习要点 接口的定义 接口作为约定 接口作为能力 接口 为什么使用接口 需求描述 要求实现防盗门的功能(防盗门:带锁的门). 需求分析 门有“开”和“关”的功能,锁有“上锁”和“开锁”的功能. 将门和 ...
- python3中shuffle函数
1. shuffle函数与其他函数不一样的地方 shuffle函数没有返回值!shuffle函数没有返回值!shuffle函数没有返回值!仅仅是实现了对list元素进行随机排序的一种功能 请看下面的坑 ...