Java [Leetcode 155]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.
解题思路:
主要是怎么保存最小值这个问题,每次在向栈中添加数据时,总是和当前的最小值比较,如果比当前的最小值小,那么则把当前的最小值压入栈中,表明当前栈中的最小值为当前最小值,然后再把新的数据压入栈中。每次弹出栈顶的元素时候,将弹出的数据与当前的最小值比较,如果相等,表明已经达到当前最小值所在的位置,下面的那个数是当前栈中的最小值,将其弹出,其值为新的最小值。
代码如下:
class MinStack {
Stack<Integer> stack = new Stack<Integer>();
int min = Integer.MAX_VALUE;
public void push(int x) {
if(x <= min){
stack.push(min);
min = x;
}
stack.push(x);
}
public void pop() {
int peek = stack.pop();
if(peek == min)
min = stack.pop();
}
public int top() {
return stack.peek();
}
public int getMin() {
return min;
}
}
Java [Leetcode 155]Min Stack的更多相关文章
- 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 ...
- leetcode 155. Min Stack --------- java
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- LeetCode 155 Min Stack(最小栈)
翻译 设计支持push.pop.top和在常量时间内检索最小元素的栈. push(x) -- 推送元素X进栈 pop() -- 移除栈顶元素 top() -- 得到栈顶元素 getMin() -- 检 ...
- Java for LeetCode 155 Min Stack
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- [LeetCode] 155. Min Stack 最小栈
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- Leetcode 155 Min Stack 小顶堆+栈,优先队列实现 难度:0
https://leetcode.com/problems/min-stack/ #include <vector> #include <queue> #include < ...
- Leetcode 155 Min Stack
题意:设计一个能输出栈内最小值的栈 该题设计两个栈,一个栈是正常的栈s,而另一个是存最小值的栈sm 在push时要判断sm是否为空,如果为空或者非空但是栈顶元素大于等于插入值的 需要在sm中插入x 同 ...
- 155. Min Stack
题目: Design a stack that supports push, pop, top, and retrieving the minimum element in constant time ...
- 【LeetCode】Min Stack 解题报告
[题目] Design a stack that supports push, pop, top, and retrieving the minimum element in constant tim ...
随机推荐
- [CFgym]2015-2016 ACM-ICPC Pacific Northwest Regional Contest小结
*感谢两位浙江大佬带我飞 贴下成绩 div2 div1 *div2不是我打的上个厕所就5/11了 比赛小结 A [题目大意] 有n(n<=500)个机场,两两之间距离是g[i][j],每经停一个 ...
- Application、Session、Cookie、ViewState的特性
http://blog.csdn.net/zyw_anquan/article/details/7664132 Application的特性: 存储的物理位置:服务器端内存. 存储的类型限制:任意 ...
- Unity3D 问题流水总结
一.error CS1612:Cannot modify a value type return value of `UnityEngine.SliderJoint2D.limits'. Consid ...
- Window 8.1 开启Wifi共享
p{padding-left:20px;} Hosted network supported:Yes 支持Wifi共享 命令:netsh wlan set hostednetwork mode=al ...
- HDU 2136 Largest prime factor(查找素数,筛选法)
题目梗概:求1000000以内任意数的最大质因数是第几个素数,其中 定义 1为第0个,2为第1个,以此类推. #include<string.h> #include<stdio.h& ...
- POJ 1731
#include<iostream> #include<string> #include<algorithm> using namespace std; int m ...
- android Notification定义与应用
首先要明白一个概念: Intent 与 PendingIntent 的区别: Intent:是意图,即告诉系统我要干什么,然后做Intent应该做的事,而intent是消息的内容 PendingInt ...
- REST_FRAMEWORK加深记忆-加了用户登陆认证,自定义权限的API接口
哈哈,终于快结束了.. urls.py from django.conf.urls import include, url from django.contrib import admin urlpa ...
- 【POJ3243】拓展BSGS(附hash版)
上一篇博文中说道了baby step giant step的方法(简称BSGS),不过对于XY mod Z = K ,若x和z并不互质,则不能直接套用BSGS的方法了. 为什么?因为这时候不存在逆元了 ...
- 表单插件——form
表单插件——form 通过表单form插件,调用ajaxForm()方法,实现ajax方式向服务器提交表单数据,并通过方法中的options对象获取服务器返回数据,调用格式如下: $(form). a ...