当前位置:网站首页>最小栈详解
最小栈详解
2022-06-30 02:23:00 【Evil_boy__】
最小栈简介:
最小栈是一种算法思路,可以在O(1)的时间复杂度中获取整个栈的最小值
最小栈思路:
我们需要两个栈,一个是主栈,一个是最小栈,如果一个数进主栈了,那么我们把这个数和最小栈的栈顶元素比较,如果比最小栈的栈顶元素小,那么这个数就入最小栈,否则就把最小栈的栈顶元素重新入最小栈

题目:最小栈
题目描述:

输入:
输出:
解释:
代码:
class MinStack {
public:
stack<int> st, stt;
MinStack() {
}
void push(int val) {
st.push(val);
if(stt.empty()){
stt.push(val);
}else{
if(stt.top() > val){
stt.push(val);
}else{
stt.push(stt.top());
}
}
}
void pop() {
st.pop();
stt.pop();
}
int top() {
return st.top();
}
int getMin() {
return stt.top();
}
};
/**
* Your MinStack object will be instantiated and called as such:
* MinStack* obj = new MinStack();
* obj->push(val);
* obj->pop();
* int param_3 = obj->top();
* int param_4 = obj->getMin();
*/
边栏推荐
- 直接插入排序
- Day_ 19 multithreading Basics
- ROS Bridge 笔记(01)— apt 安装、源码编译安装、安装依赖、运行显示
- 新考纲下的PMP考试有多难?全面解析
- 26. common interview questions of algorithm
- [MySQL 06] backup and restore MySQL database in Linux + docker container environment
- How difficult is the PMP Exam under the new syllabus? Comprehensive analysis
- DHU programming exercise
- JS reverse case -rus5 logic learning
- Select sort
猜你喜欢

Quick sort

Realization of a springboard machine

Insert sort directly

冒泡排序

26. common interview questions of algorithm

26.算法常用面试题

After the blueprint node of ue5 is copied to UE4, all connections and attribute values are lost

Créer des compétences exquises dans l'éducation des créateurs

Matlab 2012a drawing line segment with arrow

归并排序
随机推荐
How do PMP candidates respond to the new exam outline? Look!
新考纲下的PMP考试有多难?全面解析
Est - ce que la bourse en ligne est sécurisée? Dois - je ouvrir un compte pour la spéculation boursière?
PR second training notes
Tencent released the first Office Photo 23 years ago. It's so chronological
Knowledge payment cannot escape the essence of "anxiety"
AutoJS代码能加密吗?YES,AutoJS加密技巧展示
Internet Crime Complaint Center reports an increase in DDoS Attacks
学术汇报(academic presentation)/PPT应该怎么做?
【postgres】postgres 数据库迁移
快速排序
How to create a CSR (certificate signing request) file?
IBM websphere通道联通搭建和测试
代码签名、驱动签名的常见问题解答
DDoS threat situation gets worse
dhu编程练习
2.< tag-动态规划和0-1背包问题>lt.416. 分割等和子集 + lt.1049. 最后一块石头的重量 II
堆排序
Restore a 35k-55k Tencent Android Senior Engineer Interview
UE5的蓝图节点拷贝到UE4后连线和属性值全部丢失了