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 ...
随机推荐
- apache LogFormat参数说明
在apache的配置文件httpd.conf里一般都有类似于LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Refere ...
- VirtualBox下安装rhel5.5 linux系统
以前也用过VMware server和VMware workstation虚拟机,现在使用了一段时间VirtualBox,感觉它比较轻巧,很适合我,在Win7系统下用起来很方便.下面详细介绍下在Vir ...
- [Angularjs]ng-file-upload上传文件
写在前面 最近在弄文档库的H5版,就查找了下相关的上传组件,发现了ng-upload的东东,推荐给大家. 系列文章 [Angularjs]ng-select和ng-options [Angularjs ...
- 关于git提交的自己的理解
包子不才,对于码云上的git的使用,自己的理解是 这个命令用于查看,哪些文件被修改了,以及修改了哪些地方, 这个命令用于增加你新添的文件,如果该文件已经存在,那么这一步则可以省略,随后就是commit ...
- XSS攻击:获取浏览器记住的明文密码
作者:余弦(@evilcos) 0x01. XSS获取明文密码的多种方式 我已经感受到Web潮流带来的巨大革新,尤其是最近HTML5越来越火.浏览器们在客户端瓜分着这个Web OS,只要是对用户体验好 ...
- [Effective JavaScript 笔记]第50条:迭代方法优于循环
"懒"程序员才是好程序员.复制和粘贴样板代码,一但代码有错误,或代码功能修改,那么程序在修改的时候,程序员需要找到所有相同功能的代码一处处进行修改.这会使人重复发明轮子,而且在别人 ...
- [codeforces 293]A. Weird Game
[codeforces 293]A. Weird Game 试题描述 Yaroslav, Andrey and Roman can play cubes for hours and hours. Bu ...
- DButils实现查询和新增
public static Adttendance DBSql(String data) throws SQLException { String url = "j ...
- Step
php+MySQL html+css JQuery Mobile JavaScript weiPHP Sina Cloud 微信公众订阅号平台开发
- win7/ubuntu双系统下,如何恢复成win7引导及卸载ubuntu
电脑原来是win7系统,后来通过硬盘安装了Ubuntu,同时把Ubuntu设置成了开机引导项(开机时选择操作系统的界面成了紫色背景白色字体的界面),ubuntu引导开机的缺点是将来要卸载Ubuntu时 ...