当前位置:网站首页>LeetCode 0155. 最小栈
LeetCode 0155. 最小栈
2022-08-03 23:26:00 【Tisfy】
【LetMeFly】155.最小栈
力扣题目链接:https://leetcode.cn/problems/min-stack/
设计一个支持 push ,pop ,top 操作,并能在常数时间内检索到最小元素的栈。
实现 MinStack 类:
MinStack()初始化堆栈对象。void push(int val)将元素val推入堆栈。void pop()删除堆栈顶部的元素。int top()获取堆栈顶部的元素。int getMin()获取堆栈中的最小元素。
示例 1:
输入: ["MinStack","push","push","push","getMin","pop","top","getMin"] [[],[-2],[0],[-3],[],[],[],[]] 输出: [null,null,null,null,-3,null,0,-2] 解释: MinStack minStack = new MinStack(); minStack.push(-2); minStack.push(0); minStack.push(-3); minStack.getMin(); --> 返回 -3. minStack.pop(); minStack.top(); --> 返回 0. minStack.getMin(); --> 返回 -2.
提示:
-231 <= val <= 231 - 1pop、top和getMin操作总是在 非空栈 上调用push,pop,top, andgetMin最多被调用3 * 104次
方法一:map
入栈时,真正入栈的同时,用哈希表将入栈的数字累加。
例如,C++中map<int, int>默认是有序存放的,因此map<int, int>.begin()->first就是栈中元素的最小值
出栈时,真正出栈的同时,哈希表中该出栈元素的个数减 1 1 1。如果减一之后出现次数为 0 0 0,就删除掉哈希表中这一键值对。
- 时间复杂度 O ( n log n ) O(n\log n) O(nlogn),其中 n n n是操作次数。这种方法严格上不能被称为“常数时间内”。
- 空间复杂度 O ( n ) O(n) O(n)
AC代码
C++
class MinStack {
private:
map<int, int> ma;
stack<int> st;
public:
MinStack() {
}
void push(int val) {
st.push(val);
ma[val]++;
}
void pop() {
int val = st.top();
st.pop();
ma[val]--;
if (!ma[val]) {
ma.erase(val);
}
}
int top() {
return st.top();
}
int getMin() {
return ma.begin()->first;
}
};
方法二:辅助栈
出栈的顺序是由入栈决定的。
我们可以额外开辟一个“辅助栈”,每次有元素入栈后,辅助栈中入栈当前栈中的最小元素( m i n { 辅助栈 . t o p ( ) , t h i s V a l } min\{辅助栈.top(), thisVal\} min{ 辅助栈.top(),thisVal})
例如当前栈中元素为-1 -2 3(最小元素为 − 2 -2 −2),现在如果新入栈一个元素 6 6 6,那么你最小元素还是 − 2 -2 −2,就将 − 2 -2 −2压入辅助栈;如果现在入栈一个元素 − 8 -8 −8,那么最小元素就应该为 − 8 -8 −8,就往辅助栈中压入 − 8 -8 −8
原始栈:
| | | |
| -1 | | -2 |
| -2 | | -2 |
| 3 | | 3 |
+----+ +----+
栈 辅助栈
- 如果入栈 6 6 6:
| 6 | | -2 | | -1 | | -2 | | -2 | | -2 | | 3 | | 3 | +----+ +----+ 栈 辅助栈 - 如果入栈 − 8 -8 −8:
| -8 | | -8 | | -1 | | -2 | | -2 | | -2 | | 3 | | 3 | +----+ +----+ 栈 辅助栈
为了方便,可以在初始化时往辅助栈中放入元素INT_MAX以防止辅助栈.top()越界
- 时间复杂度 O ( n ) O(n) O(n),其中 n n n是操作次数
- 空间复杂度 O ( n ) O(n) O(n)
AC代码
C++
class MinStack {
private:
stack<int> realStack;
stack<int> minStack;
public:
MinStack() {
minStack.push(INT_MAX);
}
void push(int val) {
realStack.push(val);
minStack.push(min(val, minStack.top()));
}
void pop() {
realStack.pop();
minStack.pop();
}
int top() {
return realStack.top();
}
int getMin() {
return minStack.top();
}
};
同步发文于CSDN,原创不易,转载请附上原文链接哦~
Tisfy:https://letmefly.blog.csdn.net/article/details/126144246
边栏推荐
- Shell编程之循环语句与函数
- utils timer
- Super perfect version of the layout have shortcut, background replacement (solve the problem of opencv Chinese path)
- Shell 用法梳理总结
- Jar a key generation document database
- 单例模式使用饿汉式和懒汉式创建一定安全?很多人不知
- The super perfect layout has shortcut keys and background replacement
- 静态文件快速建站
- 密码学基础以及完整加密通讯过程解析
- 数据分析知识点搜集(纯粹的搜集)
猜你喜欢
随机推荐
ML's yellowbrick: A case of interpretability (threshold map) for LoR logistic regression model using yellowbrick based on whether Titanic was rescued or not based on the two-class prediction dataset
With the rise of concepts such as metaverse and web3.0, many digital forms such as digital people and digital scenes have begun to appear.
[2022强网杯] polydiv和gamemaster
Flutter教程之为什么 Flutter 是创业的最佳选择?
log4j-slf4j-impl cannot be present with log4j-to-slf4j
Storage engine written by golang, based on b+ tree, mmap
Quickly build a website with static files
override learning (parent and child)
Click the icon in Canvas App to generate PDF and save it to Dataverse
Shell 用法梳理总结
Software testing is seriously involution, how to improve your competitiveness?
ts用法大全
libnet
utils timer
Take an example of a web worker
Use tf.image.resize() and tf.image.resize_with_pad() to resize images
Republish the lab report
【RYU】rest_router.py源码解析
(PC+WAP)织梦模板不锈钢类网站
rosbridge-WSL2 && carla-win11







