155 Min Stack 最小栈
设计一个支持 push,pop,top 操作,并能在常量时间内检索最小元素的栈。
push(x) -- 将元素x推入栈中。
pop() -- 删除栈顶的元素。
top() -- 获取栈顶元素。
getMin() -- 检索栈中的最小元素。
示例:
MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.getMin(); --> 返回 -3.
minStack.pop();
minStack.top(); --> 返回 0.
minStack.getMin(); --> 返回 -2.
详见:https://leetcode.com/problems/min-stack/description/
Java实现:
方法一:
class MinStack {
private Stack<Integer> stk;
private Stack<Integer> minStk;
/** initialize your data structure here. */
public MinStack() {
stk=new Stack<Integer>();
minStk=new Stack<Integer>();
}
public void push(int x) {
stk.push(x);
if(minStk.isEmpty()){
minStk.push(x);
}else if(minStk.peek()>=x){
minStk.push(x);
}
}
public void pop() {
int top=stk.pop();
if(minStk.peek()==top){
minStk.pop();
}
}
public int top() {
return stk.peek();
}
public int getMin() {
return minStk.peek();
}
}
/**
* Your MinStack object will be instantiated and called as such:
* MinStack obj = new MinStack();
* obj.push(x);
* obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.getMin();
*/
方法二:
class MinStack {
private Stack<Integer> stk;
private Stack<Integer> minStk;
/** initialize your data structure here. */
public MinStack() {
stk=new Stack<Integer>();
minStk=new Stack<Integer>();
}
public void push(int x) {
stk.push(x);
if(minStk.isEmpty()){
minStk.push(x);
}else if(minStk.peek()>=x){
minStk.push(x);
}else{
minStk.push(minStk.peek());
}
}
public void pop() {
stk.pop();
minStk.pop();
}
public int top() {
return stk.peek();
}
public int getMin() {
return minStk.peek();
}
}
/**
* Your MinStack object will be instantiated and called as such:
* MinStack obj = new MinStack();
* obj.push(x);
* obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.getMin();
*/
155 Min Stack 最小栈的更多相关文章
- [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 最小栈 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 栈同时保存当前值和最小值 辅助栈 同步栈 不同步栈 日期 题目地 ...
- [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] Min Stack 最小栈
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- lintcode 中等题:Min stack 最小栈
题目 带最小值操作的栈 实现一个带有取最小值min方法的栈,min方法将返回当前栈中的最小值. 你实现的栈将支持push,pop 和 min 操作,所有操作要求都在O(1)时间内完成. 解题 可以定义 ...
- [LeetCode] 0155. Min Stack 最小栈 & C++Runtime加速
题目 Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. ...
- [LintCode] Min Stack 最小栈
Implement a stack with min() function, which will return the smallest number in the stack. It should ...
- 第30题:LeetCode155. Min Stack最小栈
设计一个支持 push,pop,top 操作,并能在O(1)时间内检索到最小元素的栈. push(x) -- 将元素 x 推入栈中. pop() -- 删除栈顶的元素. top() -- 获取栈顶元素 ...
- 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 ...
随机推荐
- 设计模式学习笔记——Decorator装饰模式
装饰模式的作用或动机就是,尽量避免继承,而使用关联.原因是层层继承下来,内容会越来越多,有失控的危险.就扩展性而言,用关联比用继承好.所谓的关联,A使用了B,就叫A关联了B. Component 抽象 ...
- Mac版idea使用总结
1.设置文档注释快捷键:快捷键设置里搜索 Fix doc comment 2.IDEA不显示项目project视图(转载于 https://blog.csdn.net/oyimiyangguang12 ...
- JSP复习笔记
1.注释 <!--这个注释会显示在HTML源码中--> <%--隐藏注释,不会显示在HTML源码中--%> 2.声明 <%! java声明 声明变量,方法等 %> ...
- 模板方法模式-TemplateMethod
模板方法模式:定义一个操作中的算法骨架,而将一些步骤延迟到子类中.模板方法使得子类可以不改变一个算法的机构即可以重定义算法的某些特定步骤. 模板方法模式是通过吧不变形为搬移到超类,去除子类中的重复代码 ...
- feed流,图片在左还是右的区别是
feed流设计:那些谋杀你时间APP | 人人都是产品经理 http://www.woshipm.com/pd/773523.html
- babel的安装和使用方法
要使用Babel, 我们需要nodeJS的环境和npm, 主要安装了nodeJS, npm就默认安装了 , 现在安装nodeJS很简单了, 直接下载安装就好了: 安装es-checker 在使用Bab ...
- 自定义android 音乐通知栏 ——可伸缩扩展
Android custom notification for music player Example In this tutorial, you will learn how to creat ...
- 【扬中集训Day6T1】 白日梦
[题目描述] 白日梦 (daydream.c/cpp/pas) 时间限制: 1 s 空间限制: 256 MB 题目描述 SR需要相当大的睡眠量 某日,他做了一个奇怪的梦,他梦见自己成为了怪物猎人,为 ...
- Vue之组件之间的数据传递
Vue的组件作用域都是孤立的,不允许在子组件的模板内直接引用父组件的数据,必须使用特定的方法才能实现组件之间的数据传递. 下列为在vue-cli创建项目中的操作 一·父组件向子组件传递数据 在Vue中 ...
- python-----windows下安装face_recognition库
如果直接在cmd命令界面 输入:pip install face_recognition 如下图所示: 如果第一次就会出现一系列的问题,解决此问题就安装如下步骤: 一.如果你本机没有安装vistual ...