当前位置:网站首页>剑指 Offer 30. 包含 min 函数的栈
剑指 Offer 30. 包含 min 函数的栈
2022-06-10 03:01:00 【C卷卷】
题目描述

思路
用俩个栈维护
代码1
class MinStack {
public:
// /** initialize your data structure here. */
// 剑指 Offer 30. 包含 min 函数的栈
// 定义栈的数据结构,请在该类型中实现一个能够得到栈的最小元素的 min 函数在该栈中,调用 min、push 及 pop 的时间复杂度都是 O(1)。
stack<int> A, B;
MinStack() {
}
void push(int x) {
A.push(x);
if(B.empty() || B.top() >= x) {
B.push(x);
}
}
void pop() {
if(A.top() == B.top()) {
A.pop();
B.pop();
} else {
A.pop();
}
}
int top() {
return A.top();
}
int min() {
return B.top();
}
};
/** * Your MinStack object will be instantiated and called as such: * MinStack* obj = new MinStack(); * obj->push(x); * obj->pop(); * int param_3 = obj->top(); * int param_4 = obj->min(); */
感谢阅读
边栏推荐
- How to hack user code gracefully
- C#扩展方法(方法参数里的this)
- 有关直连接口的开发
- ^28js is single threaded
- Numpy矩阵操作
- The hcie Routing & switching test is postponed to December 31, 2022
- Determinant and its properties
- Summary and implementation of NLP keyword extraction methods
- V8 global. Implementation of gc()
- Self taught scaffold - "data driven science and Engineering" by Steven L. Brunton (Chapter 5.0 - 5.4)
猜你喜欢

midway的使用教程

Why is the denominator of sample variance n-1 (unbiased estimation)?

2022.05 ESP32 空中升级 OTA

Summary and implementation of NLP keyword extraction methods

promise 介绍和实现

qiankun 如何提取出公共的依赖库

Technology dry goods | linkis practice: analysis of new engine implementation process

第六章 半导体存储器【微机原理】

新增收货地址【项目 商城】

Shift+鼠标右键没有:在此处打开命令窗口(W)
随机推荐
Uniapp obtains the unique identifier oaid, IMEI, ooid when developing an app
Drawing of common charts
86.(leaflet之家)leaflet军事标绘-直线箭头采集
TS 23.122
扩展欧几里得模板+例题
Esp32 intrinsic function / variable cannot jump to definition
command
C#扩展方法(方法参数里的this)
Numpy矩阵操作
P1082 [noip2012 improvement group] congruence equation
TS 23.122
The local storage database SQLite of the uni app mobile terminal has no storage restrictions
Why is the denominator of sample variance n-1 (unbiased estimation)?
Self taught scaffold - "data driven science and Engineering" by Steven L. Brunton (Chapter 5.0 - 5.4)
Educational Codeforces Round 129 (Rated for Div. 2)(A-D)
leetcode:305. 岛屿的数量
The hcie Routing & switching test is postponed to December 31, 2022
Using GDI to realize multi-channel video stream merging
OpenCL Memory优化
2022.05 ESP32 空中升级 OTA
