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 ...
随机推荐
- (原创)(C#随笔)IEnumerable< ICollection < IList区别
public interface IEnumerable { IEnumerator GetEnumerator(); } 再看ICollection<T> public interfac ...
- pptv web前端面试题
今天上午一考完试,就一直等待pptv的电话,结果下午就收到了pptv的通知(pptv的效率还是很不错的,之前面试官和我说在一到两周之内给回复,结果过了7天就给回复了,赞一个)因为我面试的是web前端( ...
- XML序列化反序列化—常用类
public class XMLSerializer { #region (public) xml序列化 /// <summary> /// ...
- [C++]Hello C++
最先进项目中需要用到C++做开发,所以开始学习C++,典型的眼高手低,刚开始觉得还算上手,之后越学越觉得复杂. 相比C#,C++确实需要开发者投入更多的精力去设计与维护. 以下是最近对C++开发的一些 ...
- SWT可视化设计
SWT可视化设计,可以使用Google的WindowBuilder. 在Google Code中,搜索WindowBuilder就可以看到路径. 在Eclipse中 Help--->Inst ...
- iOS学习——iOS国际化(十二)
开发的移动应用更希望获取更多用户,走向世界,这就需要应用国际化,国际化其实就是多语言.这篇文章介绍Xcode4.5以后的国际化,包括应用名国际化和应用内容国际化.如果是Xcode4.5之前版本请参考. ...
- php 简易验证码(GD库)
论坛中为了防止灌水,出现了非常多的验证码的插件.如今这里介绍一个非常easy的自己定义验证码函数,这个验证码实现的原理就是通过php扩展的gd库来实现的. 给出百度百科对验证码的定义"验证码 ...
- Direct UI
在界面开发中,眼下DirectUI是个热门的技术名称,由于众多的知名公司都是用DirectUI方式作出了非常炫丽的界面.而对于大多数熟悉Win32控件,熟悉MFC开发的开发者来说,我们应该做何选择? ...
- 智能手机的工业控制应用方案——SimpleWiFi在工业控制领域应用
智能手机的工业控制应用方案——SimpleWiFi在工业控制领域应用 先上图: 现在的智能控制都是基于微控制器,随着智能的手持终端的普及,基于智能终端的控制就会越来越普遍. WIFI便是其中的一 ...
- LeetCode My Solution: Minimum Depth of Binary Tree
Minimum Depth of Binary Tree Total Accepted: 24760 Total Submissions: 83665My Submissions Given a bi ...