LeetCode--155--最小栈(java版)
设计一个支持 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> stack1;
private Stack<Integer> stack2;
/** initialize your data structure here. */
public MinStack() {
this.stack1 = new Stack<Integer>();
this.stack2 = new Stack<Integer>();
} public void push(int x) {
this.stack1.push(x);
if(this.stack2.isEmpty()){
this.stack2.push(x);
}else if(x < this.stack2.peek()){
this.stack2.push(x);
}else{
this.stack2.push(stack2.peek());
}
} public void pop() {
this.stack1.pop();
this.stack2.pop();
} public int top() {
return this.stack1.peek();
} public int getMin() {
return this.stack2.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();
*/
2019-03-03 16:21:27
LeetCode--155--最小栈(java版)的更多相关文章
- Java实现 LeetCode 155 最小栈
155. 最小栈 设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈. push(x) – 将元素 x 推入栈中. pop() – 删除栈顶的元素. top() – 获取 ...
- LeetCode——155. 最小栈
设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈. push(x) -- 将元素 x 推入栈中. pop() -- 删除栈顶的元素. top() -- 获取栈顶元素. ...
- LeetCode 155 - 最小栈 - [数组模拟栈]
题目链接:https://leetcode-cn.com/problems/min-stack/description/ 设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的 ...
- leetcode 155. 最小栈(c++)
设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈. push(x) -- 将元素 x 推入栈中.pop() -- 删除栈顶的元素.top() -- 获取栈顶元素.get ...
- 【LeetCode】155. 最小栈
155. 最小栈 知识点:栈:单调 题目描述 设计一个支持 push ,pop ,top 操作,并能在常数时间内检索到最小元素的栈. push(x) -- 将元素 x 推入栈中. pop() -- 删 ...
- leetcode算法学习----155. 最小栈(MinStack )
下面题目是LeetCode算法155题: https://leetcode.com/problems/min-stack/ 题目1:最小函数min()栈 设计一个支持 push,pop,top 操作, ...
- LeetCode 刷题笔记 155. 最小栈(Min Stack)
tag: 栈(stack) 题目描述 设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈. push(x) -- 将元素 x 推入栈中. pop() -- 删除栈顶的元素 ...
- Leetcode题目155.最小栈(简单)
题目描述: 设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈. push(x) -- 将元素 x 推入栈中.pop() -- 删除栈顶的元素.top() -- 获取栈顶 ...
- 【LeetCode】最小栈
[问题] 设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈. push(x) -- 将元素 x 推入栈中. pop() -- 删除栈顶的元素. top() -- 获取栈 ...
随机推荐
- 【Python019--函数与过程】
一.函数与过程 1.Python只有函数没有过程 >>> def hello(): print('Hello fishC!')>>> temp = hell ...
- Firemonkey的几个特色属性(二)
3.RotationAngle 控件的旋转角度,可以通过TAnimation进行角度旋转控制. 4.RotationCenter 控件旋转的中心位置,从(0,0)到(1,1),缺省是(0.5,0.5) ...
- memalign的作用【转】
本文转载自:https://blog.csdn.net/lvwx369/article/details/41726415 转自:http://hi.baidu.com/narshben/item/ca ...
- tp框架中的一些疑点知识-6
vim自带一个目录浏览器,使用命令:E就可以调出来,实际上就是浏览器的名字就是"网络读写"netrw vim也自带了 补全功能, 启动键是 "ctrl_N" 或 ...
- Java lambda例子
简单数据类型int,跟Integer在lambda中的使用还不一样,有区别 code: package com.qhong.lambda.testDemo; import java.util.Arra ...
- How to search for ? (question mark) in Excel
The ? is a wildcard which represents a single character, and the * is a wildcard character that repr ...
- HIHOcoder 1457 后缀自动机四·重复旋律7
思路 后缀自动机题目,题目本质上是要求求出所有不同的子串的和,SAM每个节点中存放的子串互不相同,所以对于每个节点的sum,可以发现是可以递推的,每个点对子节点贡献是sum[x]*10+c*sz[x] ...
- LOJ6282 数列分块入门6(分块+暴力)
真是暴力 #include <cstdio> #include <algorithm> #include <cstring> #include <vector ...
- 论文笔记之:Deep Attributes Driven Multi-Camera Person Re-identification
Deep Attributes Driven Multi-Camera Person Re-identification 2017-06-28 21:38:55 [Motivation] 本文 ...
- [CodeForces 892A] Greed (Java中sort实现从大到小排序)
题目链接:http://codeforces.com/problemset/problem/892/A 具体的Java 中 sort实现降序排序:https://www.cnblogs.com/you ...