LeetCode155.最小栈
设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈。
- push(x) -- 将元素 x 推入栈中。
- pop() -- 删除栈顶的元素。
- top() -- 获取栈顶元素。
- getMin() -- 检索栈中的最小元素。
示例:
MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.getMin(); --> 返回 -3.
minStack.pop();
minStack.top(); --> 返回 0.
minStack.getMin(); --> 返回 -2.
class MinStack { private Stack<Integer> stack;
private Stack<Integer> stackMin;
/** initialize your data structure here. */
public MinStack() {
this.stack = new Stack<Integer>();
this.stackMin = new Stack<Integer>();
} public void push(int x) {
stack.push(x);
if (!stackMin.isEmpty()) {
int min = stackMin.peek();
if (x<=min) {
stackMin.push(x);
}
} else {
this.stackMin.push(x);
}
} public void pop() {
int value = this.stack.pop();
if (!this.stackMin.isEmpty()) {
if (value == stackMin.peek()) {
stackMin.pop();
}
}
} public int top() {
int value = this.stack.peek();
return value;
} public int getMin() {
return this.stackMin.peek();
}
} /**
* Your MinStack object will be instantiated and called as such:
* MinStack obj = new MinStack();
* obj.push(x);
* obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.getMin();
*/
LeetCode155.最小栈的更多相关文章
- [Swift]LeetCode155. 最小栈 | Min Stack
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- [LeetCode] Min Stack 最小栈
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- [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之Min Stack 实现最小栈
LeetCode相关的网上资源比较多,看到题目一定要自己做一遍,然后去学习参考其他的解法. 链接: https://oj.leetcode.com/problems/min-stack/ 题目描述: ...
- lintcode 中等题:Min stack 最小栈
题目 带最小值操作的栈 实现一个带有取最小值min方法的栈,min方法将返回当前栈中的最小值. 你实现的栈将支持push,pop 和 min 操作,所有操作要求都在O(1)时间内完成. 解题 可以定义 ...
- python实现时间o(1)的最小栈
这是毕业校招二面时遇到的手写编程题,当时刚刚开始学习python,整个栈写下来也是费了不少时间.毕竟语言只是工具,只要想清楚实现,使用任何语言都能快速的写出来. 何为最小栈?栈最基础的操作是压栈(pu ...
- LeetCode OJ:Min Stack(最小栈问题)
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- LeetCode初级算法--设计问题02:最小栈
LeetCode初级算法--设计问题02:最小栈 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.net ...
- LeetCode 155:最小栈 Min Stack
LeetCode 155:最小栈 Min Stack 设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈. push(x) -- 将元素 x 推入栈中. pop() -- ...
随机推荐
- webpack.DefinePlugin
通过配置了DefinePlugin,那么这里面的标识就相当于全局变量,你的业务代码可以直接使用配置的标识.比如,你通过下面的设置: // webpack.config.js new webpack.D ...
- LeetCode 771 Jewels and Stones 解题报告
题目要求 You're given strings J representing the types of stones that are jewels, and S representing the ...
- Python文件操作---正斜杠与反斜杠
Python中的正斜杠与反斜杠 首先,"/"左倾斜是正斜杠,"\"右倾斜是反斜杠,可以记为:除号是正斜杠一般来说对于目录分隔符,Unix和Web用正斜杠/,Wi ...
- axios的特点有哪些?
1.axios是一个基于promise的HTTP库,支持promise的所有API: 2.它可以拦截请求和响应: 3.它可以转换请求数据和响应数据,并对响应回来的内容自动转换为json类型的数据: 4 ...
- ubuntu上第一个hello程序
1.终端中输入gedit hello.c ,然后输入程序: 2.使用gcc编译器,编译出在PC上运行的hello可执行程序:gcc ./hello.c -o hello-pc; 3.使用ar ...
- 【Jmeter】if控制器+循环控制器+计数器,控制接口分支
但是我不想这么做,接口只想写一次,让循环控制器和if控制器去判断接口,执行我想要的分支.这里遇到了一个问题,if控制器通过什么去判断接下来的分支?我引入了一个计数器的概念.起始值为0,每次循环加1,将 ...
- shell 命令 set命令
set命令输出所有的变量,包括全局变量和局部变量: set-o命令显示bash Shell的所有参数配置信息 set -o nounset -u ...
- centos6.8上yum安装zabbix3.2
centos6.8上yum安装zabbix3.2 zabbix3.2安装文档:https://www.zabbix.com/documentation/3.2/manual/installation/ ...
- 1 认识开源性能测试工具jmeter
典型的性能测试工具主要有2个,Load Runner和jmeter.Load Runner是商业化的,Jmeter是开源的.下面我们认识一下开源性能测试工具jmeter. 1.jmeter是什么? A ...
- 【剑指offer】两个链表的第一个公共结点
一.题目: 输入两个链表,找出它们的第一个公共结点. 二.思路: 思路一:模拟数组,进行两次遍历,时间复杂度O(n2) 思路二:假定 List1长度: a+n List2 长度:b+n, 且 a&l ...