当前位置:网站首页>剑指 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();
*/
边栏推荐
- Overview of spark RDD
- Blue Bridge Cup embedded_ STM32_ New project file_ Explain in detail
- Bidding promotion process
- Computer graduation design PHP college student human resources job recruitment network
- Method of changing object properties
- Redis list
- Comments on flowable source code (XXXV) timer activation process definition processor, process instance migration job processor
- Use image components to slide through photo albums and mobile phone photo album pages
- SSM 程序集
- RDD creation method of spark
猜你喜欢
leetcode3、实现 strStr()
Using SA token to solve websocket handshake authentication
Using SA token to solve websocket handshake authentication
NLP fourth paradigm: overview of prompt [pre train, prompt, predict] [Liu Pengfei]
Publish your own toolkit notes using NPM
Blue Bridge Cup embedded_ STM32_ New project file_ Explain in detail
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
Jisuanke - t2063_ Missile interception
Blue Bridge Cup embedded_ STM32 learning_ Key_ Explain in detail
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
随机推荐
在线怎么生成富文本
0211 embedded C language learning
Selenium element positioning (2)
MySQL lethal serial question 1 -- are you familiar with MySQL transactions?
729. 我的日程安排表 I / 剑指 Offer II 106. 二分图
Computer graduation design PHP college student human resources job recruitment network
[solution] add multiple directories in different parts of the same word document
Spark accumulator
Accelerating spark data access with alluxio in kubernetes
RDD partition rules of spark
【clickhouse】ClickHouse Practice in EOI
How to upgrade kubernetes in place
729. My schedule I / offer II 106 Bipartite graph
安装php-zbarcode扩展时报错,不知道有没有哪位大神帮我解决一下呀 php 环境用的7.3
vs code保存时 出现两次格式化
Global and Chinese markets of screw rotor pumps 2022-2028: Research Report on technology, participants, trends, market size and share
leetcode3、实现 strStr()
Minecraft 1.16.5 生化8 模组 2.0版本 故事书+更多枪械
Shutter doctor: Xcode installation is incomplete
General process of machine learning training and parameter optimization (discussion)