当前位置:网站首页>剑指 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();
*/
边栏推荐
- Formatting occurs twice when vs code is saved
- How to set an alias inside a bash shell script so that is it visible from the outside?
- Thinking about the best practice of dynamics 365 development collaboration
- Using SA token to solve websocket handshake authentication
- Card 4G industrial router charging pile intelligent cabinet private network video monitoring 4G to Ethernet to WiFi wired network speed test software and hardware customization
- 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
- Using SA token to solve websocket handshake authentication
- Gbase 8C database upgrade error
- RDD partition rules of spark
- Know MySQL database
猜你喜欢
Executing two identical SQL statements in the same sqlsession will result in different total numbers
PHP campus financial management system for computer graduation design
PHP campus movie website system for computer graduation design
Use image components to slide through photo albums and mobile phone photo album pages
SPI communication protocol
[depth first search] Ji Suan Ke: Betsy's trip
MySQL index
The intelligent material transmission system of the 6th National Games of the Blue Bridge Cup
Derivation of Biot Savart law in College Physics
Online reservation system of sports venues based on PHP
随机推荐
[eight part essay] what is the difference between unrepeatable reading and unreal reading?
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
MySQL index
Blue Bridge Cup embedded_ STM32 learning_ Key_ Explain in detail
使用npm发布自己开发的工具包笔记
Executing two identical SQL statements in the same sqlsession will result in different total numbers
How to generate rich text online
Pangolin Library: subgraph
一题多解,ASP.NET Core应用启动初始化的N种方案[上篇]
Jisuanke - t2063_ Missile interception
Redis string type
SQL statement
RDD conversion operator of spark
Publish your own toolkit notes using NPM
Text editing VIM operation, file upload
2 power view
Computer graduation design PHP animation information website
TrueType字体文件提取关键信息
【机器人库】 awesome-robotics-libraries
Xshell 7 Student Edition