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 ...
随机推荐
- C#时间戳转换[转发]
http://www.cnblogs.com/qingliuyu/p/3835858.html 以下是C#下的日期与unix时间戳的相互转换: /// <summary> /// 日期转换 ...
- java 泛型 精析
Created by Marydon on 1.概述 泛型是Java SE 1.5的新特性,泛型的本质是参数化类型,也就是说所操作的数据类型被指定为一个参数: 这种参数类型可以用在类.接口和方法的 ...
- 通俗的理解HTTPS以及SSL中的证书验证
一.HTTPS的安全性体现在哪 HTTP(超文本传输协议,Hyper Text Transfer Protocol)是我们浏览网站信息传输最广泛的一种协议.HTTPS(Hyper Text Trans ...
- 微博混合云DCP:极端流量下的峰值应对与架构挑战
摘要: 在2016杭州云栖大会的“开发者技术峰会”上,来自新浪微博的资深运维架构师王关胜带来题为<微博混合云DCP:极端流量下的峰值应对与架构挑战>的精彩分享,分享中他从微博业务背景及峰值 ...
- Xcode编译 No such file or directory
No such file or directory 差点儿相同算是Xcode比較常见的一个编译错误了.原因往往是加入或删除美术资源的时候出错.尽管是小问题,但出现的频率非常高. 解决方法(能够依次尝试 ...
- 【laravel5.4】中jquery的post Ajax提交
1.post的ajax需要提交csrf_token字段,进行安全过滤 <meta name="csrf-token" content="{{ csrf_token( ...
- HDUOJ---三角形(组合数学)
http://acm.hdu.edu.cn/showproblem.php?pid=1249 三角形 Time Limit: 2000/1000 MS (Java/Others) Memory ...
- PAT 1085 Perfect Sequence
PAT 1085 Perfect Sequence 题目: Given a sequence of positive integers and another positive integer p. ...
- PLSQL_统计信息系列01_统计信息的概念和重要性
2014-12-18 Created By BaoXinjian
- Linux进程地址空间和虚拟内存
一.虚拟内存 先来看一张图(来自<Linux内核完全剖析>),如下: 分段机制:即分成代码段,数据段,堆栈段.每个内存段都与一个特权级相关联,即0~3,0具有最高特权级(内核),3则是最低 ...