当前位置:网站首页>剑指 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();
*/
边栏推荐
- Gbase 8C database upgrade error
- Open source | Ctrip ticket BDD UI testing framework flybirds
- 论文笔记: 极限多标签学习 GalaXC (暂存, 还没学完)
- Audio and video engineer YUV and RGB detailed explanation
- Adapter-a technology of adaptive pre training continuous learning
- Use Scrollview and tabhost to realize vertical scrollbars and tabs
- General process of machine learning training and parameter optimization (discussion)
- 更改对象属性的方法
- Grabbing and sorting out external articles -- status bar [4]
- Use image components to slide through photo albums and mobile phone photo album pages
猜你喜欢
PHP campus movie website system for computer graduation design
Tensorflow customize the whole training process
Know MySQL database
Accelerating spark data access with alluxio in kubernetes
使用npm发布自己开发的工具包笔记
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
Computer graduation design PHP college classroom application management system
TrueType字体文件提取关键信息
Visualstudio2019 compilation configuration lastools-v2.0.0 under win10 system
同一个 SqlSession 中执行两条一模一样的SQL语句查询得到的 total 数量不一样
随机推荐
Kubernetes stateless application expansion and contraction capacity
729. 我的日程安排表 I / 剑指 Offer II 106. 二分图
更改对象属性的方法
How to set an alias inside a bash shell script so that is it visible from the outside?
02. Go language development environment configuration
Global and Chinese markets of nasal oxygen tubes 2022-2028: Research Report on technology, participants, trends, market size and share
Publish your own toolkit notes using NPM
Thinking about the best practice of dynamics 365 development collaboration
D22:indeterminate equation (indefinite equation, translation + problem solution)
【clickhouse】ClickHouse Practice in EOI
[robot hand eye calibration] eye in hand
729. My schedule I / offer II 106 Bipartite graph
Blue Bridge Cup embedded_ STM32 learning_ Key_ Explain in detail
[coppeliasim] 6-DOF path planning
Use image components to slide through photo albums and mobile phone photo album pages
GBase 8c数据库升级报错
PHP campus movie website system for computer graduation design
Open source | Ctrip ticket BDD UI testing framework flybirds
[depth first search notes] Abstract DFS
Unity learning notes -- 2D one-way platform production method