LeetCode: Min Stack 解题报告
Min Stack
Question
Solution
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.
Show Tags
Have you met this question in a real interview? Yes No
SOLUTION 1:
比较直观。用一个min stack专门存放最小值,如果有比它小 或是相等的(有多个平行的最小值都要单独存放,否则pop后会出问题),
则存放其到minstack.具体看代码:
class MinStack {
Stack<Integer> elements = new Stack<Integer>();
Stack<Integer> minStack = new Stack<Integer>(); public void push(int x) {
elements.push(x);
if (minStack.isEmpty() || x <= minStack.peek()) {
minStack.push(x);
}
} public void pop() {
if (elements.isEmpty()) {
return;
} // 这个地方太蛋疼了,居然要用equals...
if (elements.peek().equals(minStack.peek())) {
minStack.pop();
}
elements.pop();
} public int top() {
return elements.peek();
} public int getMin() {
return minStack.peek();
}
}
2014.1229 redo.
class MinStack {
Stack<Integer> s = new Stack<Integer>();
Stack<Integer> min = new Stack<Integer>(); public void push(int x) {
s.push(x);
if (min.isEmpty() || x <= min.peek()) {
min.push(x);
}
} // Pop 1: use equals.
public void pop1() {
// BUG 1: Very very trick. we should use EQUALS here instead of "=="
if (s.peek().equals(min.peek())) {
min.pop();
}
s.pop();
} // Pop 2: use int
public void pop2() {
// BUG 1: Very very trick. we should use EQUALS here instead of "=="
int n1 = s.peek();
int n2 = min.peek();
if (n1 == n2) {
min.pop();
}
s.pop();
} // Pop 3: use (int)
public void pop() {
// BUG 1: Very very trick. we should use EQUALS here instead of "=="
if ((int)s.peek() == (int)min.peek()) {
min.pop();
}
s.pop();
} public int top() {
return s.peek();
} public int getMin() {
return min.peek();
}
}
GITHUB:
https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/stack/MinStack.java
LeetCode: Min Stack 解题报告的更多相关文章
- 【LeetCode】Min Stack 解题报告
[题目] Design a stack that supports push, pop, top, and retrieving the minimum element in constant tim ...
- 【原创】leetCodeOj --- Min Stack 解题报告
题目地址: https://oj.leetcode.com/problems/min-stack/ 题目内容: Design a stack that supports push, pop, top, ...
- 【LeetCode】895. Maximum Frequency Stack 解题报告(Python)
[LeetCode]895. Maximum Frequency Stack 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxueming ...
- LeetCode: Combination Sum 解题报告
Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...
- leetCode Min Stack解决共享
原标题:https://oj.leetcode.com/problems/min-stack/ Design a stack that supports push, pop, top, and ret ...
- LeetCode - Course Schedule 解题报告
以前从来没有写过解题报告,只是看到大肥羊河delta写过不少.最近想把写博客的节奏给带起来,所以就挑一个比较容易的题目练练手. 原题链接 https://leetcode.com/problems/c ...
- 【LeetCode】Permutations 解题报告
全排列问题.经常使用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. ...
- LeetCode: Sort Colors 解题报告
Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of th ...
- 【LeetCode】716. Max Stack 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双栈 日期 题目地址:https://leetcode ...
随机推荐
- Struts中的常量
以下是Struts中常量的一些经常使用配置,保存下来留作积累吧. <?xml version="1.0" encoding="UTF-8"?> &l ...
- Timus 1149. Sinus Dances 打印复杂公式
就是打印以下这两个复杂的式子: Let An = sin(1–sin(2+sin(3–sin(4+-sin(n))-) Let Sn = (-(A1+n)A2+n–1)A3+-+2)An+1 For ...
- [Done]ftp使用小结
基本命令: put 本地文件名 ftp文件名 get ftp文件名 本地文件名 mget ftp文件多个文件 注意使用该命令时先用 lcd切换本地路径 还有一些常用的 ls mkdir 等,参考 ...
- 9、java中static详解
一.static关键字的用途 在<Java编程思想>P86页有这样一段话: “static方法就是没有this的方法.在static方法内部不能调用非静态方法,反过来是可以的.而且可以在没 ...
- 如何让history命令显示最近所执行过的命令的具体执行时间
如何让history命令显示最近所执行过的命令的具体执行时间. 步骤如下: 1.以ROOT用户编辑/etc/profile文件,在里面加入下面内容(我一般习惯在最末尾加): export HISTTI ...
- 快速搭建Seeddms文档管理系统
Seddms文档管理系统是开源的 环境: Redhat6.5 lamp 01.LAMP的安装 安装请看:http://www.cnblogs.com/xiaochina/p/6442337.html ...
- cxf之java.lang.NoSuchMethodError: org.springframework.aop.support.AopUtils.isCglibProxyClass(Ljava/lang/C
想用cxf发布一个web服务,但是容器启动报这个错,求高人解答啊 [问题点数:20分,无满意结帖,结帖人shijing266] 楼主好懒,主要还是jar版本的问题,spring4.2.0以上需要使用c ...
- HDUOJ------1711Number Sequence
Number Sequence Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- Android ——真机调试
1. 设置android手机为USB调试模式.步骤: menu---> 设置 ---> 应用程序 ---> 开发 , 选择[USB调试] 2. 用USB连接手机和电脑,并确保成功.步 ...
- 快速掌握activity的生命周期
activity的生命周期不管是在面试还是在工作中我们都会经常遇到,这当然也是非常基础的,基础也很重要哦,学会activity的生命周期对我们以后开发更健壮的程序会有很大帮助.下面来看一下Activi ...