[LeetCode] 155. Min Stack 最小栈
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.
Example:
MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.getMin(); --> Returns -3.
minStack.pop();
minStack.top(); --> Returns 0.
minStack.getMin(); --> Returns -2.
这道最小栈跟原来的栈相比就是多了一个功能,可以返回该栈的最小值。使用两个栈来实现,一个栈来按顺序存储 push 进来的数据,另一个用来存出现过的最小值。代码如下:
C++ 解法一:
class MinStack {
public:
MinStack() {}
void push(int x) {
s1.push(x);
if (s2.empty() || x <= s2.top()) s2.push(x);
}
void pop() {
if (s1.top() == s2.top()) s2.pop();
s1.pop();
}
int top() {
return s1.top();
}
int getMin() {
return s2.top();
} private:
stack<int> s1, s2;
};
Java 解法一:
public class MinStack {
private Stack<Integer> s1 = new Stack<>();
private Stack<Integer> s2 = new Stack<>(); public MinStack() {}
public void push(int x) {
s1.push(x);
if (s2.isEmpty() || s2.peek() >= x) s2.push(x);
}
public void pop() {
int x = s1.pop();
if (s2.peek() == x) s2.pop();
}
public int top() {
return s1.peek();
}
public int getMin() {
return s2.peek();
}
}
需要注意的是上面的 Java 解法中的 pop() 中,为什么不能用注释掉那两行的写法,博主之前也不太明白为啥不能对两个 stack 同时调用 peek() 函数来比较,如果是这种写法,那么不管 s1 和 s2 对栈顶元素是否相等,永远返回 false。这是为什么呢,这就要看 Java 对于peek的定义了,对于 peek() 函数的返回值并不是 int 类型,而是一个 Object 类型,这是一个基本的对象类型,如果直接用双等号 == 来比较的话,肯定不会返回 true,因为是两个不同的对象,所以一定要先将一个转为 int 型,然后再和另一个进行比较,这样才能得到想要的答案,这也是 Java 和 C++ 的一个重要的不同点吧。
那么下面再来看另一种解法,这种解法只用到了一个栈,还需要一个整型变量 min_val 来记录当前最小值,初始化为整型最大值,然后如果需要进栈的数字小于等于当前最小值 min_val,则将 min_val 压入栈,并且将 min_val 更新为当前数字。在出栈操作时,先将栈顶元素移出栈,再判断该元素是否和 min_val 相等,相等的话将 min_val 更新为新栈顶元素,再将新栈顶元素移出栈即可,参见代码如下:
C++ 解法二:
class MinStack {
public:
MinStack() {
min_val = INT_MAX;
}
void push(int x) {
if (x <= min_val) {
st.push(min_val);
min_val = x;
}
st.push(x);
}
void pop() {
int t = st.top(); st.pop();
if (t == min_val) {
min_val = st.top(); st.pop();
}
}
int top() {
return st.top();
}
int getMin() {
return min_val;
}
private:
int min_val;
stack<int> st;
};
Java 解法二:
public class MinStack {
private int min_val = Integer.MAX_VALUE;
private Stack<Integer> s = new Stack<>(); public MinStack() {}
public void push(int x) {
if (x <= min_val) {
s.push(min_val);
min_val = x;
}
s.push(x);
}
public void pop() {
if (s.pop() == min_val) min_val = s.pop();
}
public int top() {
return s.peek();
}
public int getMin() {
return min_val;
}
}
Github 同步地址:
https://github.com/grandyang/leetcode/issues/155
类似题目:
参考资料:
https://leetcode.com/problems/min-stack/
https://leetcode.com/problems/min-stack/discuss/49014/java-accepted-solution-using-one-stack
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] 155. Min Stack 最小栈的更多相关文章
- [LeetCode] 0155. Min Stack 最小栈 & C++Runtime加速
题目 Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. ...
- 【LeetCode】155. Min Stack 最小栈 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 栈同时保存当前值和最小值 辅助栈 同步栈 不同步栈 日期 题目地 ...
- 155 Min Stack 最小栈
设计一个支持 push,pop,top 操作,并能在常量时间内检索最小元素的栈. push(x) -- 将元素x推入栈中. pop() -- 删除栈顶的元素. top() -- 获取 ...
- [CareerCup] 3.2 Min Stack 最小栈
3.2 How would you design a stack which, in addition to push and pop, also has a function min which r ...
- leetcode 155. Min Stack 、232. Implement Queue using Stacks 、225. Implement Stack using Queues
155. Min Stack class MinStack { public: /** initialize your data structure here. */ MinStack() { } v ...
- LeetCode 155 Min Stack(最小栈)
翻译 设计支持push.pop.top和在常量时间内检索最小元素的栈. push(x) -- 推送元素X进栈 pop() -- 移除栈顶元素 top() -- 得到栈顶元素 getMin() -- 检 ...
- [LeetCode] Min Stack 最小栈
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- lintcode 中等题:Min stack 最小栈
题目 带最小值操作的栈 实现一个带有取最小值min方法的栈,min方法将返回当前栈中的最小值. 你实现的栈将支持push,pop 和 min 操作,所有操作要求都在O(1)时间内完成. 解题 可以定义 ...
- [LintCode] Min Stack 最小栈
Implement a stack with min() function, which will return the smallest number in the stack. It should ...
随机推荐
- ubuntu 16.04 上编译和安装C++机器学习工具包mlpack并编写mlpack-config.cmake | tutorial to compile and install mplack on ubuntu 16.04
本文首发于个人博客https://kezunlin.me/post/1cd6a04d/,欢迎阅读最新内容! tutorial to compile and install mplack on ubun ...
- 在.net中读写config文件的各种方法【转】
今天谈谈在.net中读写config文件的各种方法. 在这篇博客中,我将介绍各种配置文件的读写操作. 由于内容较为直观,因此没有过多的空道理,只有实实在在的演示代码, 目的只为了再现实战开发中的各种场 ...
- 【题解】Norma [COCI2014] [SP22343]
[题解]Norma [COCI2014] [SP22343] 传送门:\(\text{Norma [COCI2014]}\) \(\text{[SP22343]}\) [题目描述] 给定一个整数 \( ...
- Caused by: java.lang.ClassNotFoundException: org.springframework.data.repository.config.BootstrapMode
1.起因,启动SpringBoot2.0的时候报了这个错误.说找不到类,咱也是刚学SpringBoot2.0,咱也不懂,咱也不知道问谁,研究一翻,找不到原因就百度了. 参考链接:https://blo ...
- War 包部署
Springboot 进行war包部署,以及踩坑历险!!! https://www.jianshu.com/p/4c2f27809571 Springboot2项目配置(热部署+war+外部tomca ...
- python3.0安装django2.0、xadmin
1.操作环境 Windows10.python3.8 2.安装django2.0 pip install django==2.0 x 1 pip install django==2.0 3.安装相 ...
- Java常用类Date相关知识
Date:类 Date 表示特定的瞬间,精确到毫秒. 在 JDK 1.1 之前,类 Date 有两个其他的函数.它允许把日期解释为年.月.日.小时.分钟和秒值.它也允许格式化和解析日期字符串. Dat ...
- .NET同一个页面父容器与子容器通信方案
主界面: 关键主页面代码: <div id="EditDiv"> <iframe src="javascript:void(0)" id=&q ...
- Windows+Qt+MinGW使用gRPC
本文参考博客文章Qt gRPC 简单应用进行了亲自尝试,特此记录以下过程,为后人提供经验.我的环境:Windows10 x64需要依赖MSYS2环境(一个类Unix环境,包管理器)MSYS2 gith ...
- iOS 快速打包方法
这种打包方式应该是目前所有打包方式中最快的,就是编译工程--找到.app文件--新建Payload文件夹--拷贝.app到Payload文件夹--压缩成zip--更改后缀名为ipa--完成! 注意事项 ...