【题目】

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.

【用Java内置的Stack实现】

来自:https://oj.leetcode.com/discuss/15659/simple-java-solution-using-two-build-in-stacks?state=edit-15691&show=15659#q15659

class MinStack {
// stack: store the stack numbers
private Stack<Integer> stack = new Stack<Integer>();
// minStack: store the current min values
private Stack<Integer> minStack = new Stack<Integer>(); public void push(int x) {
// store current min value into minStack
if (minStack.isEmpty() || x <= minStack.peek())
minStack.push(x);
stack.push(x);
} public void pop() {
// use equals to compare the value of two object, if equal, pop both of them
if (stack.peek().equals(minStack.peek()))
minStack.pop();
stack.pop();
} public int top() {
return stack.peek();
} public int getMin() {
return minStack.peek();
}
}

【分析】

这道题的关键之处就在于 minStack 的设计,push() pop() top() 这些操作Java内置的Stack都有,不必多说。

我最初想着再弄两个数组,分别记录每一个元素的前一个比它大的和后一个比它小的,想复杂了。

第一次看上面的代码,还认为它有问题,为啥仅仅在 x<minStack.peek() 时压栈?假设,push(5), push(1), push(3) 这样minStack里不就仅仅有5和1,这样pop()出1后, getMin() 不就得到5而不是3吗?事实上这样想是错的,由于要想pop()出1之前,3就已经被pop()出了。.

minStack 记录的永远是当前全部元素中最小的,不管 minStack.peek() 在stack 中所处的位置。

【不用内置Stack的实现】

来自:https://oj.leetcode.com/discuss/15651/my-java-solution-without-build-in-stack

class MinStack {
Node top = null; public void push(int x) {
if (top == null) {
top = new Node(x);
top.min = x;
} else {
Node temp = new Node(x);
temp.next = top;
top = temp;
top.min = Math.min(top.next.min, x);
}
} public void pop() {
top = top.next;
return;
} public int top() {
return top == null ? 0 : top.val;
} public int getMin() {
return top == null ? 0 : top.min;
}
} class Node {
int val;
int min;
Node next; public Node(int val) {
this.val = val;
}
}

【LeetCode】Min Stack 解题报告的更多相关文章

  1. LeetCode: Min Stack 解题报告

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

  2. 【原创】leetCodeOj --- Min Stack 解题报告

    题目地址: https://oj.leetcode.com/problems/min-stack/ 题目内容: Design a stack that supports push, pop, top, ...

  3. 【LeetCode】895. Maximum Frequency Stack 解题报告(Python)

    [LeetCode]895. Maximum Frequency Stack 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxueming ...

  4. LeetCode: Combination Sum 解题报告

    Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...

  5. leetCode Min Stack解决共享

    原标题:https://oj.leetcode.com/problems/min-stack/ Design a stack that supports push, pop, top, and ret ...

  6. LeetCode - Course Schedule 解题报告

    以前从来没有写过解题报告,只是看到大肥羊河delta写过不少.最近想把写博客的节奏给带起来,所以就挑一个比较容易的题目练练手. 原题链接 https://leetcode.com/problems/c ...

  7. 【LeetCode】Permutations 解题报告

    全排列问题.经常使用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. ...

  8. LeetCode: Sort Colors 解题报告

    Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of th ...

  9. 【LeetCode】716. Max Stack 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双栈 日期 题目地址:https://leetcode ...

随机推荐

  1. IE浏览器下web调试工具之--IE WebDeveloper介绍

    做Web项目的架构设计.开发.测试,免不了要熟悉Web页面调试工具,以此来获知哪些浏览器支持Web页面的显示,哪些浏览器下显示有问题. 目前市面上比较火爆的浏览器内核提供商,有微软的IE.mozill ...

  2. SPOJ 11840. Sum of Squares with Segment Tree (线段树,区间更新)

    http://www.spoj.com/problems/SEGSQRSS/ SPOJ Problem Set (classical) 11840. Sum of Squares with Segme ...

  3. window批处理-3.go

    go: 控制批处理中的命令运行流程 命令格式: go label lable--行号 demo bat @echo off echo 跳过中间.运行最后 goto last type a.txt :l ...

  4. vim: 搭建vim看代码的环境

    使用 vim + ctags + cscope + taglist 阅读源码  http://my.oschina.net/u/554995/blog/59927 vim tab变空格 http:// ...

  5. Qt5窗口设计

    主窗口设计通常是应用程序界面设计的第一步,主窗口主要分为窗口标题,菜单栏,工具栏和状态栏这四个部分,只要在程序设计中分别对四个项目进行设计就可以实现主窗口的编程了.在下面的例子中,我们就以一个打开文件 ...

  6. Android 讲述Help提示框

    Android 讲述Help提示框 XML/HTML代码 <stringname="help_dialog_text"> <i>Author:fonter. ...

  7. [Codecademy] HTML&CSS 第七课:CSS: An Overview

    本文出自   http://blog.csdn.net/shuangde800 [Codecademy] HTML && CSS课程学习目录 --------------------- ...

  8. CodeForces 462B Appleman and Card Game(贪心)

    题目链接:http://codeforces.com/problemset/problem/462/B Appleman has n cards. Each card has an uppercase ...

  9. 基于visual Studio2013解决C语言竞赛题之1062高与矮

       题目 解决代码及点评 /************************************************************************/ /* 62 ...

  10. spring Jdbc自己主动获取主键。

    学习了下springjdbc,感觉挺有用的,相对来说springjdbc 扩展性相当好了 package com.power.dao; import java.lang.reflect.Paramet ...