5.Min Stack(能返回最小数的栈)
Level:
Easy
题目描述:
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
- push(x) -- Push element x onto stack.
- pop() -- Removes the element on top of the stack.
- top() -- Get the top element.
- getMin() -- Retrieve the minimum element in the stack.
Example:
MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.getMin(); --> Returns -3.
minStack.pop();
minStack.top(); --> Returns 0.
minStack.getMin(); --> Returns -2.
思路分析:
使用两个栈,一个栈压入元素,另一个栈栈顶始终保存已经入栈元素中的最小值。
代码:
class MinStack {
/** initialize your data structure here. */
Stack<Integer>pushStack=new Stack<>();
Stack<Integer>minStack=new Stack<>();
public MinStack() {
}
public void push(int x) {
pushStack.push(x);
if(minStack.isEmpty()||x<=minStack.peek()){
minStack.push(x);
}
}
public void pop() {
if(!pushStack.isEmpty()){
if((int)pushStack.peek()==(int)minStack.peek()){
minStack.pop();
}
pushStack.pop();
}
}
public int top() {
if(!pushStack.isEmpty())
return pushStack.peek();
else
return -1;
}
public int getMin() {
return minStack.peek();
}
}
5.Min Stack(能返回最小数的栈)的更多相关文章
- lintcode 中等题:Min stack 最小栈
题目 带最小值操作的栈 实现一个带有取最小值min方法的栈,min方法将返回当前栈中的最小值. 你实现的栈将支持push,pop 和 min 操作,所有操作要求都在O(1)时间内完成. 解题 可以定义 ...
- 带最小值操作的栈 · Min Stack
[抄题]: 实现一个带有取最小值min方法的栈,min方法将返回当前栈中的最小值. 你实现的栈将支持push,pop 和 min 操作,所有操作要求都在O(1)时间内完成. [思维问题]: [一句话思 ...
- LeetCode 155:最小栈 Min Stack
LeetCode 155:最小栈 Min Stack 设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈. push(x) -- 将元素 x 推入栈中. pop() -- ...
- [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] 0155. Min Stack 最小栈 & C++Runtime加速
题目 Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. ...
- 栈 堆 stack heap 堆内存 栈内存 内存分配中的堆和栈 掌握堆内存的权柄就是返回的指针 栈是面向线程的而堆是面向进程的。 new/delete and malloc/ free 指针与内存模型
小结: 1.栈内存 为什么快? Due to this nature, the process of storing and retrieving data from the stack is ver ...
- [LintCode] Min Stack 最小栈
Implement a stack with min() function, which will return the smallest number in the stack. It should ...
- [LeetCode] Min Stack 栈
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- 155. Min Stack
题目: Design a stack that supports push, pop, top, and retrieving the minimum element in constant time ...
随机推荐
- paramiko分开执行多条命令 不像之前一样使用\n
#!/usr/bin/env python#-*- encoding -*- import paramiko transport = paramiko.Transport(('192.168.11.1 ...
- 问题:Oracle出发器;结果:1、Oracle触发器详解,2、Oracle触发器示例
ORACLE触发器详解 本篇主要内容如下: 8.1 触发器类型 8.1.1 DML触发器 8.1.2 替代触发器 8.1.3 系统触发器 8.2 创建触发器 8.2.1 触发器触发次序 8.2.2 创 ...
- docker 笔记 (6)搭建本地registry
转:http://blog.csdn.net/felix_yujing/article/details/51564739 新版 registry v2对镜像存储格式进行了重新设计,并且和旧版还不兼容. ...
- 【Android 多媒体应用】使用MediaCodec解码使用SurfaceView显示视频
1.MainActivity.java import android.app.Activity; import android.os.Bundle; import android.os.Environ ...
- opencv中文网站相关下载
http://wiki.opencv.org.cn/index.php/Download
- 使用LaTeX按IEEE模板写论文时的参考文献管理方法(BibTeX使用小结)
之前用LaTeX写论文时,参考文献都是手动添加管理的,真是让人很抓狂.所以这次趁着假期,简单看了一下怎么使用BibTeX对参考文献进行管理,这里以IEEE的最新模板为例. 首先说明,我之前用的是MiK ...
- Windows平台上通过git下载github的开源代码
常见指令整理: (1)检查ssh密钥是否已经存在.GitBash. 查看是否已经有了ssh密钥:cd ~/.ssh.示例中说明已经存在密钥 (2)生成公钥和私钥 $ ssh-keygen -t rsa ...
- day70-oracle 13-数据字典
实际上数据字典它就是表.这种表比较特殊,给它取个名字叫做数据字典.既然是表的话,它就是要存数据的.它存的是这些数据:用户有哪些权限,用户创建了哪些表,用户能够访问哪些表,这种信息跟员工表.部门表没有关 ...
- 【总结整理】json数据请求简化版理解(祺哥的成果)
在同源js目录下新建.txt文件 { "news":[ {"title":"审计管理","time":"201 ...
- 框架之 hibernate简单入门
hibernate框架的搭建 Hibernate框架的概述 1. Hibernate框架的概述 * Hibernate称为 * Hibernate是一个开放源代码的对象关系映射(ORM)框架,它对JD ...