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和在恒定时间内检索最小 ...
随机推荐
- 21-THREE.JS 将法线矢量映射到RGB颜色的材质
<!DOCTYPE html> <html> <head> <title></title> <script src="htt ...
- LeetCode OJ:Compare Version Numbers(比较版本字符串)
Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...
- TCPL学习毕节:第六章hash表
对于P126的哈希表构成: struct nlist *install(char *name, char *defn) { struct nlist *np; unsigned hashval; if ...
- Selenium2+Python自动化学习笔记(第1天)
参考[http://blog.csdn.net/henni_719/article/details/51096531]大神写的笔记,多谢大神共享. 哈哈,今天又找到一位大神写的Selenium2+Py ...
- 在C / C ++中清除输入缓冲区
笔者介绍:姜雪伟,IT公司技术合伙人,IT高级讲师,CSDN社区专家,特邀编辑,畅销书作者,已出版书籍:<手把手教你架构3D游戏引擎>电子工业出版社和<Unity3D实战核心技术详解 ...
- apply 无循环拼接数组
apply()第二个参数只能是数组,这个数组将作为参数传给原函数的参数列表arguments. 其实在实际开发中,JS 继承的方法并不止这一种,使用原型链继承是更加常用的方式,此外还有构造函数继承,这 ...
- Developing IOS Application with Delphi Xe4 .only for play the toy?
Recently, i am working on r&d of some keypoint of some app idea. if all thing ok, i will continu ...
- 前端之JavaScript 01
一JavaScript介绍 js历史 1992年Nombas开发出C-minus-minus(C--)的嵌入式脚本语言(最初绑定在CEnvi软件中).后将其改名ScriptEase.(客户端执行的语言 ...
- flash游戏服务器安全策略
在网页游戏开发中,绝大多数即时通信游戏采用flash+socket 模式来作为消息数据传递.在开发过程中大多数开发者在开发过程中本地没有问题,但是一旦部署到了网络,就存在连接上socket服务器.究 ...
- Http基础(记忆笔记)
地址解析:http://localhost.com:8080/index.htm 协议名:Http 主机名:localhost.com 端口:8080 对象路径:/index.htm 通过域名解析lo ...