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 ...
随机推荐
- 原生的ado.net(访问sql server数据库)
本文介绍原生的ado.net(访问sql server数据库) 写在前面 数据库连接字符串 过时的写法 string str = "server=localhost;database=my_ ...
- 第十二章 Jetty的工作原理解析(待续)
Jetty的基本架构 Jetty的启动过程 接受请求 处理请求 与JBoss集成 与Tomcat的比较
- Android 4学习(1):学习路线图
学习路线图 如下图所示,整个Android的架构可以分为四层,五个部分.我给自己制定的学习路线图是这样的: 对于有java基础的入门级android开发者而言,首先要学会使用Application F ...
- 问题:不支持Dictionary;结果:在Web Service中傳送Dictionary
在Web Service中傳送Dictionary 有個需求,想在Web Service中傳遞Dictionary<string, string>參數,例如: 排版顯示純文字 [WebMe ...
- DAY8-python异常处理
一.什么是异常 异常就是程序运行时发生错误的信号(在程序出现错误时,则会产生一个异常,若程序没有处理它,则会抛出该异常,程序的运行也随之终止),在python中,错误触发的异常如下 而错误分两种 #语 ...
- 1 ffmpeg介绍
重点讲解解码的过程.FFmpeg可以进行X264编码.软编码效率是非常低的.即时你编VGA的话它的效率也很低.
- koa2 原生链接mysql
1.安装mysql $ npm install mysql 2.代码示例: const mysql = require("mysql"); // mysql.Promise = g ...
- ngx-bootstrap使用03 Alerts组件、利用Object.assign复制对象
1 Alerts 该组件用于给用户操作提供反馈信息或者提供一些警告信息 2 用法 2.1 下载ngx-bootstrap依赖 参考博文:点击前往 2.2 在模块级别导入AlertModule模块 技巧 ...
- input标签name、value与id属性
id是唯一标识符,不允许有重复值可以通过它的值来获得对应的html标签对象.相当于人的身份证具有唯一性 name:控件的名字,一个控件是否设置它的 name 属性是不会影响到这个网页的功能实现的.但是 ...
- R语言输出pdf时,中文乱码处理
本文转载自:https://blog.csdn.net/hongweigg/article/details/47907555 1.使用基础包,使用函数pdf()输出 在使用pdf()函数时,要输出中文 ...