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 ...
随机推荐
- 位图索引:原理(BitMap index)
http://www.cnblogs.com/LBSer/p/3322630.html 位图(BitMap)索引 前段时间听同事分享,偶尔讲起Oracle数据库的位图索引,顿时大感兴趣.说来惭愧,在这 ...
- 刨根问底拦不住——I/O模型
前言 看过很多资料,很多对I/O模型概念模糊甚至错误,希望这篇文章有助理解I/O,欢迎讨论和纠正 参考资料:<UNIX网络编程卷1> P122 I/O中涉及概念 介绍阻塞非阻塞,同步异步 ...
- Android SDK安装Android4.0“冰激淋三明治”(IceCreamSandwich)教程(转载)
昨天,Google举行了发布会,发布了Nexus Prime手机和Android4.0-IceCreamSandwich手机系统.作为Google旗下Android的最新版本手机系 统,Android ...
- linux下的struct sigaction
工作中使用案例: struct sigaction act; act.sa_sigaction = handleSignal; act.sa_flags = SA_SIGINFO; sigemptys ...
- Linux命令sed
如果一个文本文件数据比较多,大概有40万条数据,我想取出第500-1000条数据,保存到另一个文件,用linux命令该如何操作? sed -n '500,1000p' 41w.txt > new ...
- Hello Kitty微信主题很可爱?小心财产安全!
个性化是产品服务的一个趋势.微信很火,可为什么微信主题只有一个呢?你让那些小女生情何以堪?这时HelloKitty微信主题.大嘴猴.哆啦A梦等一大批主题在网上出现了,有些打着免费的旗号却做着盗号的勾当 ...
- 我所理解cocos2d-x 3.6 lua --使用Cocos Studio
Cocos是触控科技推出的游戏开发一站式解决方案,包含了从新建立项.游戏制作.到打包上线的全套流程. 开发者可以通过cocos快速生成代码.编辑资源和动画,最终输出适合于多个平台的游戏产品. Coco ...
- PV公式
IP(独立IP): 即Internet Protocol,指独立IP数.00:00-24:00内相同IP地址之被计算一次.PV(访问量): 即Page View, 即页面浏览量或点击量,用户每次刷 ...
- Apache服务器常规操作
导读 Apache是世界上排名第一的Web服务器,50%以上的Web服务器都在使用Apache,它几乎可以在所有计算机平台上运行.下面就由我给大家说说Apache服务器的一些常规操作. Apache服 ...
- CPU供电维修