当前位置:网站首页>最小栈详解
最小栈详解
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();
*/
边栏推荐
- Learning C language from scratch day 026
- How to create a CSR (certificate signing request) file?
- 网上炒股安全么?炒股需要开户吗?
- DHU programming exercise
- Jenkins continuous integration environment build 8 (configure mailbox server to send build results)
- NCA: the nine year old has launched a DDoS attack
- Radware warns about the next round of DDoS Attacks
- Is online stock trading safe? Do you need to open an account for stock trading?
- 走进江苏作家诗人胭脂茉莉|世界读书日
- Creating exquisite skills in maker Education
猜你喜欢

Creating exquisite skills in maker Education

Merge sort

Traffic, but no sales? 6 steps to increase website sales

AutoJS代码能加密吗?YES,AutoJS加密技巧展示

26.算法常用面试题

Bubble sort

ROS Bridge 笔记(01)— apt 安装、源码编译安装、安装依赖、运行显示

CTF introductory learning (WEB direction)

桶排序
![[on] [DSTG] dynamic spatiotemporalgraph revolutionary neural networks for traffic data impact](/img/c3/f9d6399c931a006ca295bb1e3ac427.png)
[on] [DSTG] dynamic spatiotemporalgraph revolutionary neural networks for traffic data impact
随机推荐
Tencent released the first Office Photo 23 years ago. It's so chronological
AutoJS代碼能加密嗎?YES,AutoJS加密技巧展示
[NPM] solve the problem of error reporting when installing typeorm with NPM
代码签名、驱动签名的常见问题解答
33Mysql
银行的理财产品一般期限是多久?
Network neuroscience——网络神经科学综述
Jupyter notebook displays a collection of K-line graphs
Alphassl digital certificate
How does payment splitting help B2B bulk commodity transactions?
Recommendations for agileplm database parameter optimization
Restore a 35k-55k Tencent Android Senior Engineer Interview
有流量,但没有销售?增加网站销量的 6 个步骤
Day_ 19 multithreading Basics
DHU programming exercise
DDoS "fire drill" service urges companies to prepare
UE5的蓝图节点拷贝到UE4后连线和属性值全部丢失了
vs实现快速替换功能
7 — filter
DHU programming exercise