【题目】

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. window下svn注册为本地的服务

    sc create svnservice binpath= "\"C:\program files\Subversion\bin\svnserve.exe\" --ser ...

  2. c++11 新特性之 autokeyword

    C++11是对眼下C++语言的扩展和修正.C++11包含大量的新特性:包含lambda表达式,类型推导keywordauto.decltype,和模板的大量改进. g++编译c++11命令加上 -st ...

  3. PHP脚本监控程序

    #!/bin/sh # Find ip IP=`/sbin/ifconfig eth1 | grep 'inet addr' | awk '{ print substr($2, index($2, & ...

  4. VC实现URL编解码器

    //变化UTF8为了中国 void UTF8ToGB(CString& szstr) { WCHAR* strSrc; TCHAR* szRes; int i = MultiByteToWid ...

  5. PHP - 递归函数

    /** * factorial($num) 计算阶乘 * @param string $num * @return string $total */ function factorial($num) ...

  6. [转]Cocos Studio和Cocos2d-x版本对应关系

    2015-1-19阅读139 评论0 From: http://www.cocoachina.com/bbs/read.php?tid=182077 版本对应列表: Studio2.x CocosSt ...

  7. 认识到了x64程序的必要性

    假如我做一个程序,在运行过程中需要使用一个Map,然而这个Map存储了超多信息的话,系统内存不够就会崩溃了.以前的解决方案可能是把内容存储在一个文件/数据库里,但是有内存岂不是更方便.更直截了当!

  8. 【C语言】在两个数成对出现的数组中找到一个单独的数。

    //在两个数成对出现的数组中找到一个单独的数.比如{1,2,3.3,1,4.2},即找出4 #include <stdio.h> int find(int arr[], int len) ...

  9. 对 sql server 数据库的备份进行加密

    原文:对 sql server 数据库的备份进行加密 嗯,最近在研究数据库备份相关的东西,考虑到应该为数据库备份加个密,就准备从网上搜索一下看看有什么好办法,没想到还挺乱... 首先,我从网上搜到的, ...

  10. SPSS Modeler数据挖掘项目实战(数据挖掘、建模技术)

    SPSS Modeler是业界极为著名的数据挖掘软件,其前身为SPSS Clementine.SPSS Modeler内置丰富的数据挖掘模型,以其强大的挖掘功能和友好的操作习惯,深受用户的喜爱和好评, ...