【题目】

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. Java网络编程:利用apache的HttpClient包进行http操作

    本文介绍如何利用apache的HttpClient包进行http操作,包括get操作和post操作. 一.下面的代码是对HttpClient包的封装,以便于更好的编写应用代码. import java ...

  2. highcharts dynamic change line color

    mouseOut: function(){ this.series.graph.attr({"stroke","#ccc"}) }

  3. WCF技术剖析之一:通过一个ASP.NET程序模拟WCF基础架构

    原文:WCF技术剖析之一:通过一个ASP.NET程序模拟WCF基础架构 细算起来,已经有好几个月没有真正的写过文章了.近半年以来,一直忙于我的第一本WCF专著<WCF技术剖析>的写作,一直 ...

  4. android 设置字体颜色、EditText自己主动输入转换成大写字母的多种方式

    在TextView上面设置某一个字的字体颜色为指定颜色时,能够通过java类SpannableString类和Html语言来实现. (一)SpannableString类方式 private void ...

  5. TinkPHP+WAMP

    1.下载wamp 我下载的是php5.5版本:根据你自身的需要嘛 http://www.wampserver.com/ 2.下载thinkphp 我下载的版本是3.2 http://www.think ...

  6. 单选按钮易忽略的Group属性

    Group就其意思就是一组的意思.就是说用于选择多个控件组合,选了TRUE后,你就可以为这组新建一个变量.把一组控件当一个控件来使用.例如多个单选按钮用group属性,这样你就可以用一个变量来管理这些 ...

  7. Mac+IPAD上使用wireshark抓包

    首先先下载wireshark和Xquartz wireshark地址: http://www.wireshark.org/download.html Xquartz 地址:http://xquartz ...

  8. C语言总结之---关键字

    我记得我开始学习C语言的时候,那时候还在读高中,我们老师就把C语言的关键字,全部写在黑板上,老师说我们下面的两节课的内容就是(把它给记下来) 你还记得标准C有多少个关键字吗? 第一:关键字描述 C99 ...

  9. javascript——ajax应用

    概念 AJAX 指异步JavaScript及XML(Asynchronous JavaScript And XML).Ajax的核心是JavaScript对象XmlHttpRequest.XmlHtt ...

  10. mysql 执行计划走索引

    <pre name="code" class="html">mysql> desc AssignClientManager; +------- ...