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 ...
随机推荐
- tensorflow serving GPU编译问题
编译gpu版本:bazel build -c opt --config=cuda --spawn_strategy=standalone //tensorflow_serving/model_serv ...
- idea 设置黑色背景
idea 设置黑色背景 CreateTime--2018年4月26日10:32:38 Author:Marydon 设置-->Appearance-->Theme调为:Darcula- ...
- java 加密工具(产生证书)
给Tomcat服务器应用加密: 命令:keytool -genkey -alias tomcat -keyalg RSA -genkey产生密钥对 -alias取得别名 -keyalg RSA产生密钥 ...
- MySQL Desc指令相关
MySQL Desc指令相关 2011-08-09 11:25:50| 分类: my基本命令 |举报 |字号 订阅 1.desc tablename; 例如 :mysql> desc jo ...
- JS正则判断输入框是否仅仅含有汉字、字母和数字
代码如下: if($.trim($("#user_api_register_form").find("input[name='user_name']").val ...
- JSON与Bean互转
转自: 关于json与javaBean之间的转换 废话不多说,直接进入主题,json与javaBean之间的互相转换 javaBean转换成json 直接使用提供的方法可以得到json JSONObj ...
- PHP 大数自动转换为科学计数法
前段时间碰到一个很头疼的问题,就是大于12位的数字在PHP中,会自动转成科学计数法表 示. 比如 1234567891234 显示为 1.23456789123E+12 , 最后只能在计算出大数之后, ...
- WCF与WPF
1. WCF(Windows Communication Foundation )是一个统一的,可用于建立安全,可靠的面向服务的应用高效的开发平台.WCF是构建安全可靠的事务性服务的统一框架.它是一种 ...
- Python2 字典 has_key() 方法
描述 Python2 字典 has_key() 方法用于判断键(key)是否存在于字典(D)中,如果键在字典中返回True,否则返回False. 官方文档推荐用 in 操作符,因为它更短更通俗易懂.h ...
- Python 元组 count() 方法
描述 Python 元组 count() 方法用于统计某个元素在元祖中出现的次数. 语法 count() 方法语法: T.count(obj) 参数 obj -- 元祖中统计的对象. 返回值 返回元素 ...