leetCode Min Stack解决共享
原标题: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.
题目事实上不难。可是是leetCode今天刚刚新增的一个题目。索性抢个头彩把解题过程分享出来:
直接上AC代码:
public class MinStack {
private int min = Integer.MAX_VALUE;
private int minIndex = -1;
private ArrayList<Integer> stack = new ArrayList<Integer>();
private int length = 0;
private HashMap<Integer,Integer> secondMinMap = new HashMap<Integer, Integer>();
public void push(int x) {
stack.add(x);
if(x <= min) { //注意这里犯过错误,须要加=号,原因是当栈空的时候假设push一个Integer.MaxValue的话。须要将此时的minIndex变为0而不是继续为-1。
//否则的话。这样的情况下。当push第二个数的时候,将出现secondMap.put(1,-1)的情况,进而当pop这个数的时候,会出现stack.get(-1)操作。
//换句话说。这个secondMap里的值仅仅有当key=0的时候。才干为-1,其它情况必须有能指向数组里位置的值
secondMinMap.put(length, minIndex); //这里存的 length位置相应的第二小的,仅仅考虑比length小的索引即可
//由于用到的场景是当前这个假设被pop了,说明它上面的全部都已经被pop了
//这个时候假设当前是min。那么接下来仅仅须要在它以下去找第二小的即可了
minIndex = length;
min = x;
}
length ++;
}
public void pop() {
if(length == 0) return;
if(minIndex == length-1) {
if(minIndex == 0) {
minIndex = -1;
min = Integer.MAX_VALUE;
} else {
minIndex = secondMinMap.get(length-1);
secondMinMap.remove(length-1);
min = stack.get(minIndex);
}
}
stack.remove(length-1);
length--;
}
public int top() {
return stack.get(length-1);
}
public int getMin() {
return min;
}
public static void main(String[] args) {
MinStack stack = new MinStack();
stack.push(2147483646);
stack.push(2147483646);
stack.push(2147483647);
System.out.println(stack.top());
stack.pop();
System.out.println(stack.getMin());
stack.pop();
System.out.println(stack.getMin());
stack.pop();
stack.push(2147483647);
System.out.println(stack.top());
System.out.println(stack.getMin());
stack.push(-2147483648);
System.out.println(stack.top());
System.out.println(stack.getMin());
stack.pop();
System.out.println(stack.getMin());
}
}
版权声明:本文博主原创文章。博客,未经同意不得转载。
leetCode Min Stack解决共享的更多相关文章
- LeetCode: Min Stack 解题报告
Min Stack My Submissions Question Solution Design a stack that supports push, pop, top, and retrievi ...
- [LeetCode] 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 ...
- LeetCode——Min Stack
Description: Design a stack that supports push, pop, top, and retrieving the minimum element in cons ...
- LeetCode() Min Stack 不知道哪里不对,留待。
class MinStack { public: MinStack() { coll.resize(2); } void push(int x) { if(index == coll.size()-1 ...
- [leetcode] Min Stack @ Python
原题地址:https://oj.leetcode.com/problems/min-stack/ 解题思路:开辟两个栈,一个栈是普通的栈,一个栈用来维护最小值的队列. 代码: class MinSta ...
- [LeetCode] Min Stack 栈
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- LeetCode Min Stack 最小值栈
题意:实现栈的四个基本功能.要求:在get最小元素值时,复杂度O(1). 思路:链表直接实现.最快竟然还要61ms,醉了. class MinStack { public: MinStack(){ h ...
- Min Stack [LeetCode 155]
1- 问题描述 Design a stack that supports push, pop, top, and retrieving the minimum element in constant ...
随机推荐
- SQL查询数据封装JavaBean对象
public static List getListBySql(String sql, Class cls){ List list = new ArrayList(); Connection ...
- Contact类解析
Contact类 public static class Contacts implements BaseColumns, ContactsColumns, ContactOptionsColumns ...
- javascript (四) 改变html样式
<h1 id="domo"> this is testing test</h1> <script> function changecolor() ...
- 与众不同 windows phone (28) - Feature(特性)之手机方向, 本地化, 应用程序的试用体验, 系统主题资源, 本地数据的加密解密
原文:与众不同 windows phone (28) - Feature(特性)之手机方向, 本地化, 应用程序的试用体验, 系统主题资源, 本地数据的加密解密 [索引页][源码下载] 与众不同 wi ...
- Python的对象和类型
Python使用对象来存储数据,构造任何类型的值都是一个对象. 任何一个对象都有三个特性:身份,类型和值. 身份是对象的唯一标识,可以通过内建函数id()得到,这个值可以认为是该对象的内存地址. Py ...
- Memcached 群集高可用性(HA)架构
Memcache本身并不实现集群功能.假设你想使用Memcahce集群需要使用第三方软件或编程来实现自己的设计,这里将被用来memagent实现代理,memagent也被称为magent.我们注意到, ...
- HelloGithub
<HelloGithub月刊>第一期 <HelloGithub月刊> 因为现在这个项目只有我自己做,只敢叫“月刊”,希望有志同道合者,快点加入到这个项目中来!同时,如果您有 ...
- 定义自己的布局RelativeLayout
绘制网格线
在Android画线必须由一个载体,无论是控制,无论是布局.实际上它们是从继承View.由画线的方式自己的控制或布局的定义是最常见的. 以下是在其定义中的小样本实现RelativeLayout绘制网络 ...
- TestApe - Unit testing for embedded software
TestApe - Unit testing for embedded software About this site Welcome - This site is TestApe.com. Mos ...
- 通过memcached来实现对tomcat集群中Session的共享策略
近期在做一套集群的实现,实现的方案是在Linux下完成对Apache + Tomcat 负载均衡的功能. 上述功能已经实现,有需要了解的朋友可以看我另外一篇博文. Linux下Apache与Tomca ...