当前位置:网站首页>最小栈详解
最小栈详解
2022-06-30 02:23:00 【Evil_boy__】
最小栈简介:
最小栈是一种算法思路,可以在O(1)的时间复杂度中获取整个栈的最小值
最小栈思路:
我们需要两个栈,一个是主栈,一个是最小栈,如果一个数进主栈了,那么我们把这个数和最小栈的栈顶元素比较,如果比最小栈的栈顶元素小,那么这个数就入最小栈,否则就把最小栈的栈顶元素重新入最小栈

题目:最小栈
题目描述:

输入:
输出:
解释:
代码:
class MinStack {
public:
stack<int> st, stt;
MinStack() {
}
void push(int val) {
st.push(val);
if(stt.empty()){
stt.push(val);
}else{
if(stt.top() > val){
stt.push(val);
}else{
stt.push(stt.top());
}
}
}
void pop() {
st.pop();
stt.pop();
}
int top() {
return st.top();
}
int getMin() {
return stt.top();
}
};
/**
* Your MinStack object will be instantiated and called as such:
* MinStack* obj = new MinStack();
* obj->push(val);
* obj->pop();
* int param_3 = obj->top();
* int param_4 = obj->getMin();
*/
边栏推荐
猜你喜欢

什么是自签名证书?自签名SSL证书的优缺点?

2.< tag-动态规划和0-1背包问题>lt.416. 分割等和子集 + lt.1049. 最后一块石头的重量 II

How to use SMS to deliver service information to customers? The guide is here!

Select sort

SSL证书七大常见错误及解决方法

8 — router

NPDP产品经理国际认证考试报名有什么要求?

AutoJS代码能加密吗?YES,AutoJS加密技巧展示

JS reverse case -rus5 logic learning

FDA ESG规定:必须使用数字证书保证通信安全
随机推荐
选择排序
Insert sort directly
True love forever valentine's Day gifts
DDoS attacks and destructive ripple effects against online gamers
CA数字证书包含哪些文件?如何查看SSL证书信息?
Jupyter notebook displays a collection of K-line graphs
DDoS "fire drill" service urges companies to prepare
【npm】解决使用npm安装TypeORM的报错问题
学术汇报(academic presentation)/PPT应该怎么做?
26.算法常用面试题
DHU programming exercise
[MySQL 05] SUSE 12 SP5 modifies the MySQL password for the first time after installing MySQL
Recommendations for agileplm database parameter optimization
Four, forty, fourhundred swatches
The birth of the cheapswap protocol
打造创客教育中精湛技艺
C语言 pivot_root的Invalid argument错误解决方案
Realization of a springboard machine
JS advanced -h5 new features
SiteLock九个常见问题