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 ...
随机推荐
- Java初学(一)
一.初识Java 1.JVM:Java跨平台是基于JVM(Java虚拟机)的,JVM不是跨平台的,针对不同平台有对应的JVM软件 2.JRE:Java开发出来的软件如果要运行还需要在环境中安装JRE( ...
- day4作业之信息表
实在是太low了,终究是自己写的,记录下 #!/usr/bin/env python # coding=utf8 import os, re #这里我把查询这块分为3个函数了,纠结了很久是放一起还是分 ...
- Developing a plugin framework in ASP.NET MVC with medium trust
http://shazwazza.com/post/Developing-a-plugin-framework-in-ASPNET-with-medium-trust.aspx January 7, ...
- Linux下修改计算机名
SuSe操作系统: 1. 修改/etc/HOSTNAME 文件 ,其内容为计算机名. 输入命令:vi /etc/HOSTNAME 使用键盘上的 x 键一个一个删除所有内容 ,然后使用键盘上的 i ...
- php的urlencode()URL编码函数浅析
URLEncode:是指针对网页url中的中文字符的一种编码转化方式,最常见的就是Baidu.Google等搜索引擎中输入中文查询时候,生成经过Encode过的网页URL. URLEncode的方 ...
- WhatsApp值160亿美元,腾讯推大众点评微信支付!
腾讯前脚刚入股大众点评,FB后脚就将斥资160亿美元收购WhatsApp(40亿美元现金和120亿美元股票). 为什么WhatsApp值160亿美元?这是什么东东呢?WhatsApp这款服务可以帮助用 ...
- Hadoop集群(第1期)_CentOS安装配置
CentOS 是什么? CentOS是一个基于Red Hat 企业级 Linux 提供的可自由使用的源代码企业级的 Linux 发行版本.每个版本的 CentOS 都会获得七年的支持(通过安全更新方式 ...
- spring所需包下载
1.进入http://repo.spring.io/webapp/search/artifact/选择artifacts,在搜过栏输入spring-framework,点击查询出来的表头artifac ...
- 6个关于dd命令备份Linux系统的例子
数据丢失带来的损失是相当昂贵的.关键数据的丢失会对各种规模的企业带来影响.有几种方法来备份Linux系统,包括rsync的和rsnapshot等.本文提供有关使用dd命令备份Linux系统的6个实例. ...
- xcode SVN
Mac上SVN的管理工具: CornerStone http://blog.csdn.net/wohaoxuexi/article/details/8444184 步骤: 1. import 项目到s ...