当前位置:网站首页>剑指 Offer 30. 包含min函数的栈
剑指 Offer 30. 包含min函数的栈
2022-07-06 02:11:00 【anieoo】
solution:
用堆栈存储pair数据, first保存入栈元素,second保存最小值
class MinStack {
public:
/** initialize your data structure here. */
typedef pair<int,int> PII;
stack<PII> stk;
MinStack() {
}
//.first保存栈元素,.second保存最小值
void push(int x) {
if(stk.empty() || x < stk.top().second) {
stk.push({x, x});
return;
}
stk.push({x, stk.top().second});
}
void pop() {
stk.pop();
}
int top() {
return stk.top().first;
}
int min() {
return stk.top().second;
}
};
/**
* 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();
*/
边栏推荐
- Flutter Doctor:Xcode 安装不完整
- 2 power view
- 使用npm发布自己开发的工具包笔记
- Overview of spark RDD
- Flowable source code comments (36) process instance migration status job processor, BPMN history cleanup job processor, external worker task completion job processor
- Computer graduation design PHP campus restaurant online ordering system
- Leetcode3, implémenter strstr ()
- 2022 edition illustrated network pdf
- 729. 我的日程安排表 I / 剑指 Offer II 106. 二分图
- How to set an alias inside a bash shell script so that is it visible from the outside?
猜你喜欢
Initialize MySQL database when docker container starts
Selenium waiting mode
Computer graduation design PHP enterprise staff training management system
[robot library] awesome robots Libraries
Leetcode3. Implement strstr()
Derivation of Biot Savart law in College Physics
【机器人库】 awesome-robotics-libraries
【社区人物志】专访马龙伟:轮子不好用,那就自己造!
使用npm发布自己开发的工具包笔记
It's wrong to install PHP zbarcode extension. I don't know if any God can help me solve it. 7.3 for PHP environment
随机推荐
Initialize MySQL database when docker container starts
【社区人物志】专访马龙伟:轮子不好用,那就自己造!
Global and Chinese market of wheelchair climbing machines 2022-2028: Research Report on technology, participants, trends, market size and share
Global and Chinese markets hitting traffic doors 2022-2028: Research Report on technology, participants, trends, market size and share
Have a look at this generation
leetcode3、实现 strStr()
[width first search] Ji Suan Ke: Suan tou Jun goes home (BFS with conditions)
RDD creation method of spark
Gbase 8C database upgrade error
更改对象属性的方法
RDD conversion operator of spark
Minecraft 1.18.1, 1.18.2 module development 22 Sniper rifle
[flask] official tutorial -part2: Blueprint - view, template, static file
Minecraft 1.16.5 生化8 模组 2.0版本 故事书+更多枪械
Computer graduation design PHP college student human resources job recruitment network
Sword finger offer 12 Path in matrix
RDD partition rules of spark
0211 embedded C language learning
【coppeliasim】高效传送带
同一个 SqlSession 中执行两条一模一样的SQL语句查询得到的 total 数量不一样