原标题: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解决共享的更多相关文章

  1. LeetCode: Min Stack 解题报告

    Min Stack My Submissions Question Solution Design a stack that supports push, pop, top, and retrievi ...

  2. [LeetCode] Min Stack 最小栈

    Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...

  3. [LeetCode] Min Stack

    Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...

  4. LeetCode——Min Stack

    Description: Design a stack that supports push, pop, top, and retrieving the minimum element in cons ...

  5. LeetCode() Min Stack 不知道哪里不对,留待。

    class MinStack { public: MinStack() { coll.resize(2); } void push(int x) { if(index == coll.size()-1 ...

  6. [leetcode] Min Stack @ Python

    原题地址:https://oj.leetcode.com/problems/min-stack/ 解题思路:开辟两个栈,一个栈是普通的栈,一个栈用来维护最小值的队列. 代码: class MinSta ...

  7. [LeetCode] Min Stack 栈

    Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...

  8. LeetCode Min Stack 最小值栈

    题意:实现栈的四个基本功能.要求:在get最小元素值时,复杂度O(1). 思路:链表直接实现.最快竟然还要61ms,醉了. class MinStack { public: MinStack(){ h ...

  9. Min Stack [LeetCode 155]

    1- 问题描述 Design a stack that supports push, pop, top, and retrieving the minimum element in constant ...

随机推荐

  1. uva 10131 Is Bigger Smarter?(DAG最长路)

    题目连接:10131 - Is Bigger Smarter? 题目大意:给出n只大象的属性, 包括重量w, 智商s, 现在要求找到一个连续的序列, 要求每只大象的重量比前一只的大, 智商却要小, 输 ...

  2. android电池充电以及电量检测驱动分析

    前段时间比较烦躁,各种不想学习不想工作,于是休息了几天.这几天又下来任务了--调试充电电路和电池电量检测电路,于是又开始工作,顺便把调试过程记录下来. 平台: cpu        飞思卡尔imx6q ...

  3. 最短路知识点总结(Dijkstra,Floyd,SPFA,Bellman-Ford)

    Dijkstra算法: 解决的问题: 带权重的有向图上单源最短路径问题.且权重都为非负值.如果采用的实现方法合适,Dijkstra运行时间要低于Bellman-Ford算法. 思路: 如果存在一条从i ...

  4. oc深坑測试题及其答案

    一.选择题(共80题,每题1分) 1. 不会立马使引用计数器改变的是: 答案:(C)  A.release  B.alloc  C.autorelease  D.retain 2. 在OC中类的接口声 ...

  5. 近期在调用 calendar.js 的时候出现中文乱码! 解决方式

    近期写一个小项目的时候:在调用 calendar.js  的时候出现中文乱码! 如图所看到的: 原因在于: 我的jsp 页面,指定的是 UTF-8 编码,然而,调用的 calendar.js 的编码确 ...

  6. SWT可视化设计

    SWT可视化设计,可以使用Google的WindowBuilder. 在Google Code中,搜索WindowBuilder就可以看到路径. 在Eclipse中   Help--->Inst ...

  7. AWS(0) - Amazon Web Services

    Computer EC2 – Virtual Servers in the Cloud EC2 Container Service – Run and Manage Docker Containers ...

  8. 位运算之 C 与或非异或

    与运算:& 两者都为1为1,否则为0 1&1=1,  1&0=0,  0&1=0,  0&0=0   或运算:| 两者都为0为0,否则为1 1|1 = 1,   ...

  9. 全国各大 oj 分类题集...

    各种题集从易到难刷到手软  你准备好了吗? 准备剁手吧

  10. hdu4521 小明系列的问题——小明序列(LIS变种 (段树+单点更新解决方案))

    链接: huangjing 题目:中文题目 思路: 1:这个题目假设去掉那个距离大于d的条件,那么必定是一个普通的LIS.可是加上那个条件后就变得复杂了.我用的线段树的解法. . .就是採用延迟更新的 ...