LeetCode之Min Stack 实现最小栈
LeetCode相关的网上资源比较多,看到题目一定要自己做一遍,然后去学习参考其他的解法。
链接: https://oj.leetcode.com/problems/min-stack/
题目描述:
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.
设计一个最小栈,支持入栈,出栈,获取栈顶元素,获取栈最小值,要求时间复杂度0(1).
Stack(栈)是First in-Last out的数据结构。如果不考虑时间复杂度,实现题目的要求都比较简单,现在限定了不超过常量时间O(1),
就不能用简单的排序过滤实现了。
另外,栈顶(top)指的是允许操作数据的一端,要与堆栈中高低地址不同的栈顶和栈底区别开来,以前我经常搞混。
public class MinStack {
//声明数据栈
private Stack<Integer> elementsStack=new Stack<Integer>();
//声明辅助栈
private Stack<Integer> supportStack=new Stack<Integer>();
/**
* 考虑到时间复杂度的需求,添加一个辅助栈,
* 每次入栈时将元素分别存入数据栈和辅助栈,
* 辅助栈中的数据始终保持最小值在栈顶,需要获取最小值时,直接Peek()辅助栈即可。
*/
public static void main(String[] args){
MinStack minStack=new MinStack();
//以下测试用例
minStack.push(0);
minStack.push(1);
minStack.push(0);
System.out.print(minStack.getMin());
minStack.pop();
System.out.print(minStack.getMin());
}
public void push(int x) {
//始终保持辅助栈顶是最小元素
if(supportStack.empty() || x <= supportStack.peek()){
supportStack.push(x);
}
elementsStack.push(x);
}
public void pop() {
//更新辅助栈
if(elementsStack.peek().equals(supportStack.peek())){
supportStack.pop();
}
elementsStack.pop();
}
public int top() {
return elementsStack.peek();
}
public int getMin() {
//辅助栈
return supportStack.peek();
}
}
提交,可以AC.
LeetCode之Min Stack 实现最小栈的更多相关文章
- LeetCode 155 Min Stack(最小栈)
翻译 设计支持push.pop.top和在常量时间内检索最小元素的栈. push(x) -- 推送元素X进栈 pop() -- 移除栈顶元素 top() -- 得到栈顶元素 getMin() -- 检 ...
- LeetCode OJ:Min Stack(最小栈问题)
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- Leetcode 946. Validate Stack Sequences 验证栈序列
946. Validate Stack Sequences 题目描述 Given two sequences pushed and popped with distinct values, retur ...
- [LeetCode] 155. Min Stack 最小栈
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- [LeetCode] 0155. Min Stack 最小栈 & C++Runtime加速
题目 Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. ...
- 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 ...
- leetCode(45):Min Stack
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- 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 ...
随机推荐
- [转载]高效使用matlab之四:一个加速matlab程序的例子
原文地址:http://www.bfcat.com/index.php/2012/11/speed-up-app/ 这篇文章原文是matlab网站上的,我把它翻译过来同时自己也学习一下.原文见这里 这 ...
- 织梦DedeCMS调用二级子栏目或者多级栏目解决方法
本文是关于织梦DedeCMS调用多级子栏目的,拿来测试分享下.DEDECMS v5.7,后台已建栏目目录,如下图1所示: 图1 DEDECMS后台栏目结构 现在,我们先来调用顶级栏目"站长新 ...
- C++_Eigen函数库用法笔记——Advanced Initialization
The comma initializer a simple example join and block initialize join two row vectors together ini ...
- Gradle用户指南(1)-Gradle安装
前置条件 Gradle 需要 Java JDK 或者 JRE,版本是 6 及以上.Gradle 将会装载自己的 Groovy 库,因此,Groovy 不需要被安装.任何存在的 Groovy 安装都会被 ...
- 基于HttpListener的web服务器
写在前面 前面两篇文章分别介绍了基于原始socket的web服务器和基于tcpListener的web服务器,本篇文章将继续介绍另外一种基于HttpListener的. HttpListener Ht ...
- C++中虚函数的作用浅析
虚函数联系到多态,多态联系到继承.所以本文中都是在继承层次上做文章.没了继承,什么都没得谈. 下面是对C++的虚函数这玩意儿的理解. 一, 什么是虚函数(如果不知道虚函数为何物,但有急切的想知道,那你 ...
- smarty中math函数的用法
在查看项目代码时,发现smart代码中有这样写. {if $auctionInfo.printingNum} 印数:{math equation="cid/1000" cid=$a ...
- Ubuntu无法关机解决办法
说明:如果不成功请参考一下文章最后的内容,也许会有帮助. 其实不止在ubuntu里面,fedora里面我也遇到了这个问题,就是电脑可以重启,但是不能直接关机,否则就一直停在关机界面,需手动关机.郁闷很 ...
- 简单聊下Unicode和UTF-8
今晚听同事分享提到这个,简单总结下. ## Unicode字符集 Unicode的出现是因为ASCII等其他编码码不够用了,比如ASCII是英语为母语的人发明的,只要一个字节8位就能够表示26个英文字 ...
- 微信新版支持读取iPhone M7/M8协处理器运动数据 与好友PK一下运动量吧
iPhone的创新是有目共睹的,Healthkit的推出预示着苹果进军健康领域,iPhone M7/M8协处理器可以收集和分析用户的健康数据,那么好的硬件自然不会被势在打造完整生态圈的微信给错过,这不 ...