当前位置:网站首页>力扣155题,最小栈
力扣155题,最小栈
2022-07-26 22:25:00 【瀛台夜雪】
力扣155题,最小栈
题目描述
设计一个支持 push ,pop ,top 操作,并能在常数时间内检索到最小元素的栈。
实现 MinStack 类:
MinStack() 初始化堆栈对象。
void push(int val) 将元素val推入堆栈。
void pop() 删除堆栈顶部的元素。
int top() 获取堆栈顶部的元素。
int getMin() 获取堆栈中的最小元素。
输入输出样例
输入:
["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.
解法一:不考虑时间复杂度,使用vector
class MinStack{
public:
MinStack()
{
nums={
};
}
void push(int val)
{
nums.push_back(val);
}
void pop()
{
nums.pop_back();
}
int top()
{
// cout<<*(nums.end()-1)<<endl;
return *(nums.end()-1);
}
int getMin()
{
int length=nums.size();
int minValue=nums[0];
for(int i=0;i<length;i++)
{
minValue=min(minValue,nums[i]);
}
// cout<<minValue<<endl;
return minValue;
}
private:
vector<int>nums;
};
解法二:使用辅助栈
//使用辅助栈,一个作为数据栈,一个作为最小栈
class MinStack2{
private:
stack<int>dataStk;
stack<int>minStk;
public:
MinStack2()
{
//最小栈先初始化入栈一个最大值
minStk.push(INT_MAX);
}
void push(int val)
{
dataStk.push(val);
minStk.push(min(minStk.top(),val));
}
void pop()
{
dataStk.pop();
minStk.pop();
}
int top()
{
// cout<<dataStk.top()<<endl;
return dataStk.top();
}
int getMin()
{
// cout<<minStk.top()<<endl;
return minStk.top();
}
};
解法三,不使用辅助栈,并不使用额外空间,堆栈保存差值
//不适用额外空间
class MinStack3
{
private:
int minValue=NULL;
stack<long>dataStk;
public:
MinStack3()
{
}
void push(int val)
{
if(dataStk.empty())
{
dataStk.push(0L);
minValue=val;
}
else
{
//数据栈中保存的是数字和最小值的差
dataStk.push(long(val)-minValue);
minValue=min(val,minValue);
}
}
//如果插值diff大于等于0,说明要出栈的值大于等于当前的min,则要出栈的值在入栈的时候没有更新min,返回min+diff
// 如果插值diff小于0,说明当前要出栈的值就是min(因为入栈的时候我们选择的就是min和入栈元素的最小值),同时,通过min-diff计算出之前min
//要注意的是diff可能会超出int范围,类似于 Integer.MAX_VALUE - 1 这种,所以diff要用Long存
void pop()
{
long diff=dataStk.top();
dataStk.pop();
if(diff>=0)
{
}
else
{
int res=minValue;
minValue=minValue-diff;
}
}
int top()
{
long diff=dataStk.top();
if(diff>=0)
{
// cout<<minValue+diff<<endl;
return minValue+diff;
}
else{
// cout<<minValue<<endl;
return minValue;
}
}
int getMin()
{
// cout<<minValue<<endl;
return minValue;
}
};
边栏推荐
- 【flask高级】结合源码分析flask中的线程隔离机制
- Practical project: boost search engine
- What are the use cases in the Internet of things industry in 2022?
- Kt6368a Bluetooth chip development precautions and problem collection - long term update
- 1. Configuration environment and project creation
- After working for one year, I have some insights (written in 2017)
- The nature and proof of the center of gravity of [mathematics] tree
- Sign up now | frontier technology exploration: how to make spark stronger and more flexible
- Use Arthas to locate online problems
- [flask advanced] analyze the thread isolation mechanism in flask in combination with the source code
猜你喜欢

科研太忙无法顾家?陈婷:人生不能只有一个支点

HCIA-R&S自用笔记(23)DHCP

About statefulwidget, you have to know the principle and main points!

第二部分—C语言提高篇_6. 多维数组

Silicon Valley class lesson 7 - Tencent cloud on demand management module (2)
![[postgresql]postgresqlg use generate_ Series() function completes statistics](/img/62/893986eb97a61f4e9ef32abc8d2a90.png)
[postgresql]postgresqlg use generate_ Series() function completes statistics

第二部分—C语言提高篇_8. 文件操作

实战项目:Boost搜索引擎
![[MySQL] CentOS 7.9 installation and use mysql-5.7.39 binary version](/img/70/5638080a2d2eabf6ae1f2a8db3abe6.png)
[MySQL] CentOS 7.9 installation and use mysql-5.7.39 binary version

Part II - C language improvement_ 12. Packaging and use of dynamic / precision Library
随机推荐
org.yaml.snakeyaml.scanner. ScannerException: mapping values are not allowed here in ‘reader‘, line
HCIA-R&S自用笔记(19)VLAN配置及实验、VLAN间路由
Customer case | student education relies on observation cloud to create a new ecosystem of observable Smart Education
Dao:op token and non transferable NFT are committed to building a new digital democracy
C.Net timestamp and time conversion support time zone
Part II - C language improvement_ 7. Structure
Six challenges facing enterprise data governance!
After working for one year, I have some insights (written in 2017)
Science | University of Washington uses AI and structural prediction to design new proteins
电脑开机后内存占用过高(50%以上)
HCIA-R&S自用笔记(18)园区网架构基础、交换机工作原理、VLAN原理
[Luogu] p2709 little B's inquiry
Import of MySQL data
[shader realizes swaying effect _shader effect Chapter 4]
Part II - C language improvement_ 6. Multidimensional array
json格式化小工具--pyqt5实例
Vit:vision transformer super detailed with code
MySQL random paging to get non duplicate data
Learn various details and thoughts of chatroom implementation in Muduo
29、 Implementation of xv6 file system (GDB tracks mkfs, buffer cache and log)