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 ...
随机推荐
- 函数响应式编程(FRP)框架--ReactiveCocoa
由于工作原因,有段时间没更新博客了,甚是抱歉,只是,从今天開始我又活跃起来了,哈哈,于是决定每周更新一博.大家互相学习.交流. 今天呢.讨论一下关于ReactiveCocoa,这个採用函数响应式编程( ...
- ebay分布式事务方案中文版
http://cailin.iteye.com/blog/2268428 不使用分布式事务实现目的 -- ibm https://www.ibm.com/developerworks/cn/clou ...
- HTTP所承载的货物(图像、文本、软件等)要满足的条件
HTTP所承载的货物(图像.文本.软件等)要满足的条件: •可以被正确识别 通过Content-Type 首部说明媒体格式,Content-Language 说明语言,以便浏览器和其他客户端能正确处理 ...
- JVM内存管理、JVM垃圾回收机制、新生代、老年代以及永久代
内存模型 JVM运行时数据区由程序计数器.堆.虚拟机栈.本地方法栈.方法区部分组成,结构图如下所示. JVM内存结构由程序计数器.堆.栈.本地方法栈.方法区等部分组成,结构图如下所示: 1)程序计数器 ...
- Linux生成高强度密码
在撰写,自动化脚本.往往需要添加账户及密码.如何自动化填写随机密码,有点意思.... 01.openssl生成密码 [root@mvp ~]# openssl rand -base 14Usage: ...
- TP3.2之引入第三方类库文件和普通.php文件
1.引入第三方类库 .class.php文件 1.1 类库有写namespace命名空间 namespace Org\Util; class Auth { } 保存到ThinkPHP/Library/ ...
- HDUOJ ---1269迷宫城堡
http://acm.hdu.edu.cn/showproblem.php?pid=1269 迷宫城堡 Time Limit: 2000/1000 MS (Java/Others) Memory ...
- window.postMessage实现网页间通信
window.postMessage() 方法可以安全地实现跨域通信.通常,对于两个不同页面的脚本,只有当执行它们的页面位于具有相同的协议(通常为https),端口号(443为https的默认值),以 ...
- 使用Python的turtle库实现七段数码管绘制
七段数码管绘制:七段数码管是由7段数码管拼接而成,每段有亮或不亮两种情况,改进的七段数码管还包括一个小数点位置.七段数码管能形成2=128种状态,其中部分状态能够显示易于人们理解的数字或字母含义.因此 ...
- 【LeetCode】129. Sum Root to Leaf Numbers (2 solutions)
Sum Root to Leaf Numbers Given a binary tree containing digits from 0-9 only, each root-to-leaf path ...