LeetCode题解 #155 Min Stack
写一个栈,支持push pop top getMin
难就难在在要在常量时间内返回最小的元素。
一开始乱想了很多东西,想到了HashMap,treeMap,堆什么的,都被自己一一否决了。
后来想到其实用一个栈来记录当前的最小值就好了,只有当被删除的元素等于min栈的栈顶元素时,才删除min栈里面的元素。
min栈中每个元素代表着那一段栈中的最小值了。
// stack: store the stack numbers
private Stack<Integer> stack = new Stack<Integer>();
// minStack: store the current min values
private Stack<Integer> minStack = new Stack<Integer>();
public void push(int x) {
// store current min value into minStack
if (minStack.isEmpty() || x <= minStack.peek())
minStack.push(x);
stack.push(x);
}
public void pop() {
// use equals to compare the value of two object, if equal, pop both of them
if (stack.peek().equals(minStack.peek()))
minStack.pop();
stack.pop();
}
public int top() {
return stack.peek();
}
public int getMin() {
return minStack.peek();
}
LeetCode题解 #155 Min Stack的更多相关文章
- 【LeetCode】155. Min Stack 最小栈 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 栈同时保存当前值和最小值 辅助栈 同步栈 不同步栈 日期 题目地 ...
- 【leetcode】155 - Min Stack
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- 【一天一道LeetCode】#155. Min Stack
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Design ...
- leetcode 155. Min Stack 、232. Implement Queue using Stacks 、225. Implement Stack using Queues
155. Min Stack class MinStack { public: /** initialize your data structure here. */ MinStack() { } v ...
- [LeetCode] 155. Min Stack 最小栈
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- leetcode 155. Min Stack --------- java
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- Java [Leetcode 155]Min Stack
题目描述: Design a stack that supports push, pop, top, and retrieving the minimum element in constant ti ...
- 155. Min Stack
题目: Design a stack that supports push, pop, top, and retrieving the minimum element in constant time ...
- LeetCode算法题-Min Stack(Java实现)
这是悦乐书的第177次更新,第179篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第36题(顺位题号是155).设计一个支持push,pop,top和在恒定时间内检索最小 ...
随机推荐
- FFT快速傅立叶变换
//最近突然发现博客园支持\(\rm\LaTeX\),非常高兴啊! 话说离省选只有不到五天了还在学新东西确实有点逗…… 切到正题,FFT还是非常神奇的一个东西,能够反直觉地把两个多项式相乘的时间复杂度 ...
- 【2018年全国多校算法寒假训练营练习比赛(第五场)-C】字符串问题(KMP)
题目链接:https://www.nowcoder.com/acm/contest/77/C [题意] 求一个字符串中最长的子串,要求子串既是原串的前缀又是后缀,除前后缀还在中间出现过. [思路] K ...
- Android性能优化系列总篇
目前性能优化专题已完成以下部分: 性能优化总纲——性能问题及性能调优方式 性能优化第四篇——移动网络优化 性能优化第三篇——Java(Android)代码优化 性能优化第二篇——布局优化 性能优化第一 ...
- 在Ubuntu 14.04 TLS下openvas V8.0源代码安装过程
[Qboy原创]详细记录源代码的安装过程 1.下载原代码 在http://www.openvas.org/install-source.html下载 Libraries 8.0.8 Scanner 5 ...
- Visual Studio Nuget还原步骤
vs2013是在这里还原: NuGet套件还原步骤(以vs2012为例) 下载别人的范例,出现由于Nuget套件不存在而无法启动时:效果如下图: 步骤如下:1.点击 项目->启用NuGet程序包 ...
- 手把手教你怎么用ArcgisOnline发布地图服务
Arcgis推出了Arcgis Online,但是大家都不知道这是个什么东西,怎么用这个东西,今天这篇文章手把手的教你如何使用Arcgisonline发布地图服务. 一.ArcgisOnline简介 ...
- unity 事件顺序及功能说明
unity3d中所有控制脚本的基类MonoBehaviour有一些虚函数用于绘制中事件的回调,也可以直接理解为事件函数,例如大家都很清楚的Start,Update等函数,以下做个总结. Awake 当 ...
- JavaScript test//href
目录 JavaScript test//href JavaScript test//href href 其实这个问题并不属于这里的.但是呢,由于一天晚上因为这个问题扰我"一夜不能眠" ...
- "==" 与 "is"的区别
Is there a difference between `==` and `is` in Python? "=="是比较内容相当;"is"是比较对象的id是 ...
- Socket通信简单实例(WCF调用Socket)
服务端: 控制台程序监听 /// <summary> /// Server /// </summary> class Program { static Socket serve ...