Lintcode12-Min Stack-Easy
2. Min Stack
Implement a stack with following functions:
push(val)push val into the stackpop()pop the top element and return itmin()return the smallest number in the stack
All above should be in O(1) cost.
Example
Example 1:
Input:
push(1)
pop()
push(2)
push(3)
min()
push(1)
min()
Output:
1
2
1
Notice
min() will never be called when there is no number in the stack.
思路:
因为O(1),所以用两个栈操作,stack 和 minStack。
push(int num): stack push; 如果minStack为空,minStack直接push,或者 num<=minStack的栈顶元素,minStack也push
pop(): stack pop; 如果minStack的栈顶值和stack栈顶值相等(Integer类判断两个值相等要用equals.()),minStack也pop.
min(): 返回minStack 栈顶值。
注意:
(1)push的时候,num == minStack.peek() 时,也要minStack.peek()。[line 12]
否则,有可能出现EmptyStackException:第一次pop()后,minStack为空了,再min()就会有异常。
push(1)
push(1)
push(1)
min()
pop()
min()
pop()
(2)equals 和 ==
public class MinStack {
private Stack<Integer> stack;
private Stack<Integer> minStack;
public MinStack() {
stack = new Stack<Integer>();
minStack = new Stack<Integer>();
}
public void push(int number) {
stack.push(number);
if (minStack.empty() || minStack.peek() >= number) { //相等时,minStack也要push
minStack.push(number);
}
}
public int pop() {
if (stack.peek().equals(minStack.peek())) //比较栈顶值,只能用equals()
minStack.pop();
return stack.pop();
}
public int min() {
return minStack.peek();
}
}
Lintcode12-Min Stack-Easy的更多相关文章
- LeetCode算法题-Min Stack(Java实现)
这是悦乐书的第177次更新,第179篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第36题(顺位题号是155).设计一个支持push,pop,top和在恒定时间内检索最小 ...
- [LintCode] Min Stack 最小栈
Implement a stack with min() function, which will return the smallest number in the stack. It should ...
- [CareerCup] 3.2 Min Stack 最小栈
3.2 How would you design a stack which, in addition to push and pop, also has a function min which r ...
- leetcode 155. Min Stack --------- java
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- Min Stack [LeetCode 155]
1- 问题描述 Design a stack that supports push, pop, top, and retrieving the minimum element in constant ...
- Min Stack
Min Stack Design a stack that supports push, pop, top, and retrieving the minimum element in constan ...
- 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解决共享
原标题:https://oj.leetcode.com/problems/min-stack/ Design a stack that supports push, pop, top, and ret ...
- 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 ...
随机推荐
- hdu 4544——消灭兔子
游戏规则很简单,用箭杀死免子即可. 箭是一种消耗品,已知有M种不同类型的箭可以选择,并且每种箭都会对兔子造成伤害,对应的伤害值分别为Di(1 <= i <= M),每种箭需要一定的QQ币 ...
- Javaweb实现对mongodb的增删改查(附带源代码)
运行截图: 删除后的信息 项目源代码:https://www.cnblogs.com/post/readauth?url=/zyt-bg/p/9807396.html
- vue路由动态加载
注意:是动态加载不是动态路由 解决的问题: 动态配置菜单栏的路由参数--实现菜单级的权限控制 问题成因: 在vue实例化的时候vuex.vue-router 就需要加载完毕,无法使用异步的方式从服务器 ...
- ORACLE导入梗
1.Oracle版数据库的安装及初始化 1.1安装oracle数据库(10g或11g) 1.2以用户system账号登陆oralcle数据库的sqlplus,执行以下语句 1.3创建表空间语句: cr ...
- ML.NET 0.9特性简介
ML.NET 0.9已于上周发布,距离上次0.8版本的发布只有一个多月,此次增加的新特性主要包括特征贡献计算,模型可解释性增强,ONNX转换对GPU的支持,Visual Studio ML.NET项目 ...
- 如何在Ubuntu上安装腾讯QQ
首先QQ国际版下载连接:http://pan.baidu.com/s/1sj7i6BF 安装步骤: 一:安装依赖库 在终端输入:sudo apt-get install libgtk2.0-0:i3 ...
- java 编写函数将字符串的首尾空格删除。
String 类有个方法去除字符串首位空格: str.trim(); 查看源代码: public String trim() { int len = value.length; ; char[] va ...
- 图像分类(三)GoogLenet Inception_v3:Rethinking the Inception Architecture for Computer Vision
Inception V3网络(注意,不是module了,而是network,包含多种Inception modules)主要是在V2基础上进行的改进,特点如下: 将滤波器尺寸(Filter Size) ...
- 前端的图片压缩image-compressor(可在图片上传前实现图片压缩)
https://www.imooc.com/article/40038 https://www.jianshu.com/p/3ce3e3865ae2 前端的图片压缩image-compressor(可 ...
- DAX/PowerBI系列 - 参数表(Parameter Table) 度量值模板
DAX/PowerBI系列 - 参数表(Parameter Table) 度量值模板 难度: ★★☆☆☆(2星) 适用范围: ★★★☆☆(3星) 概况: 当你有多个度量值都需要计算YTD,MoM,而又 ...