当前位置:网站首页>包含min函数的栈(js)
包含min函数的栈(js)
2022-07-30 05:56:00 【一枚小银子】
定义栈的数据结构,请在该类型中实现一个能够得到栈的最小元素的 min 函数在该栈中,调用 min、push 及 pop 的时间复杂度都是 O(1)。
示例:
MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.min(); --> 返回 -3.
minStack.pop();
minStack.top(); --> 返回 0.
minStack.min(); --> 返回 -2.思路分析:
要能得到栈内最小元素,容易想到用一个变量来存这个元素,但问题是这个元素出去以后,下一个最小元素呢?解决该问题就得专门用一个数组来存第一小第二小第三小...的元素们了,定义两个数组,主数组arr1用来做栈的出栈和入栈,副数组arr2用来存小元素。重点来了,
入栈时,若该元素小于等于当前最小元素,那该元素也要往arr2中放;
出栈时,若该元素是当前最小元素,arr2尾部的该元素也要一同出来;
由此,arr2中存的元素越往尾部越小,每次读尾部最后一个元素就是最小元素。
代码实现:
var MinStack = function() {
this.arr1 = []
this.arr2 = []
};
/**
* @param {number} x
* @return {void}
*/
MinStack.prototype.push = function(x) {
this.arr1.push(x)
if (!this.arr2.length || (this.arr2.length && x <= this.arr2[this.arr2.length - 1])) {
this.arr2.push(x)
}
};
/**
* @return {void}
*/
MinStack.prototype.pop = function() {
const e = this.arr1.pop()
if (this.arr2[this.arr2.length - 1] === e) {
this.arr2.pop()
}
return e
};
/**
* @return {number}
*/
MinStack.prototype.top = function() {
return this.arr1[this.arr1.length - 1]
};
/**
* @return {number}
*/
MinStack.prototype.min = function() {
return this.arr2[this.arr2.length - 1]
};边栏推荐
- The newcomer deleted data by mistake, and the team leader skillfully used MySQL master-slave replication to delay the recovery of losses
- Vue2进阶篇-编程式路由导航、缓存路由组件、路由的激活与失活
- 空间顶点到平面的距离计算的证明及其源码
- The introduction of AI meta-learning into neuroscience, the medical effect is expected to improve accurately
- 深度学习:线性回归模型
- No, the Log4j vulnerability hasn't been fully fixed yet?
- STL source code analysis: conceptual understanding of iterators, and code testing.
- Polygon 3D(三维平面多边形)的法向量的计算(MeshLab默认的计算)
- STL源码剖析:bound friend template friend代码测试和理解
- 【雷达目标检测】恒定阈值法和恒虚警(CFAR)法及代码实现
猜你喜欢

The calculation and source code of the straight line intersecting the space plane

golang : Zap日志整合

export , export default,import完整用法

STL源码剖析:临时对象的代码测试和理解

Playing script killing with AI: actually more involved than me

The calculation proof of the intersection of the space line and the plane and its source code

When does MySQL use table locks and when does it use row locks?

STL源码剖析:class template explicit specialization代码测试和理解

mysql高阶语句(一)

The first artificial intelligence safety competition officially launched
随机推荐
The Society of Mind - Marvin Minsky
Ali two sides: Sentinel vs Hystrix comparison, how to choose?
New material under the plastic restriction order - polylactic acid (PLA)
头条二面:MySQL中有几种常见的 SQL 错误用法?
Equation Derivation Proof of Vector Triple Product
Develop common tool software
空间顶点到平面的距离计算的证明及其源码
Electron之初出茅庐——搭建环境并运行第一个程序
New breakthrough in artificial muscle smart materials
How does Redis prevent oversold and inventory deduction operations?
ETL为什么经常变成ELT甚至LET?
Let the "label" content in Baidu map generator expand--solution
assert
学生成绩管理系统(C语言)
五号黯区靶场 mysql 注入之limit注入记录
深度学习:线性回归模型
Electron中设置菜单(Menu),主进程向渲染进程共享数据
Camera coordinate system, world coordinate system, pixel coordinate system conversion, and Fov conversion of OPENGLDEFocal Length and Opengl
DHCP principle and configuration
相机坐标系,世界坐标系,像素坐标系三者转换,以及OPENGLDEFocal Length和Opengl 的 Fov转换