当前位置:网站首页>剑指 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();
*/
边栏推荐
- MySQL learning notes - subquery exercise
- Open source | Ctrip ticket BDD UI testing framework flybirds
- [robot library] awesome robots Libraries
- Kubernetes stateless application expansion and contraction capacity
- Using SA token to solve websocket handshake authentication
- 阿里测开面试题
- leetcode-2.回文判断
- Text editing VIM operation, file upload
- GBase 8c数据库升级报错
- PHP campus financial management system for computer graduation design
猜你喜欢
[robot library] awesome robots Libraries
leetcode3、实现 strStr()
Minecraft 1.18.1、1.18.2模组开发 22.狙击枪(Sniper Rifle)
抓包整理外篇——————状态栏[ 四]
Leetcode sum of two numbers
TrueType字体文件提取关键信息
Exness: Mercedes Benz's profits exceed expectations, and it is predicted that there will be a supply chain shortage in 2022
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
500 lines of code to understand the principle of mecached cache client driver
Blue Bridge Cup embedded_ STM32_ New project file_ Explain in detail
随机推荐
Global and Chinese markets of screw rotor pumps 2022-2028: Research Report on technology, participants, trends, market size and share
Ali test open-ended questions
[width first search] Ji Suan Ke: Suan tou Jun goes home (BFS with conditions)
Social networking website for college students based on computer graduation design PHP
Exness: Mercedes Benz's profits exceed expectations, and it is predicted that there will be a supply chain shortage in 2022
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
TrueType字体文件提取关键信息
Campus second-hand transaction based on wechat applet
Numpy array index slice
Adapter-a technology of adaptive pre training continuous learning
Have a look at this generation
Minecraft 1.18.1、1.18.2模组开发 22.狙击枪(Sniper Rifle)
RDD partition rules of spark
Extracting key information from TrueType font files
Bidding promotion process
Unity learning notes -- 2D one-way platform production method
通过PHP 获取身份证相关信息 获取生肖,获取星座,获取年龄,获取性别
The intelligent material transmission system of the 6th National Games of the Blue Bridge Cup
Tensorflow customize the whole training process
Blue Bridge Cup embedded_ STM32 learning_ Key_ Explain in detail