当前位置:网站首页>最小栈详解
最小栈详解
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();
*/
边栏推荐
猜你喜欢
Digicert、Sectigo、Globalsign代码签名证书的区别
[Galaxy Kirin V10] [desktop] Firefox browser settings home page does not take effect
SSL证书格式转化的两种方法
什么是自签名证书?自签名SSL证书的优缺点?
Illustration Google V8 19: asynchronous programming (II): how does V8 implement async/await?
主流CA吊销俄罗斯数字证书启示:升级国密算法SSL证书,助力我国网络安全自主可控
DMX configuration
Playful palette: an interactive parametric color mixer for artists
如何制作CSR(Certificate Signing Request)文件?
IBM websphere通道联通搭建和测试
随机推荐
新考纲下的PMP考试有多难?全面解析
Blue Bridge Cup stm32g431 - three lines of code for keys (long press, short press, click, double click)
AutoJS代码能加密吗?YES,AutoJS加密技巧展示
如何使用SMS向客户传递服务信息?指南在这里!
隐藏在科技教育中的steam元素
Jenkins continuous integration environment build 8 (configure mailbox server to send build results)
[Galaxy Kirin V10] [desktop] Firefox browser settings home page does not take effect
Encapsulate a complete version of the uniapp image and video upload component, which can be used immediately, switch between images and videos, customize the upload button style, delete the button sty
Est - ce que la bourse en ligne est sécurisée? Dois - je ouvrir un compte pour la spéculation boursière?
[论]【DSTG】Dynamic SpatiotemporalGraph Convolutional Neural Networks for Traffic Data Imputation
云存储架构能解决 DevOps 的什么问题?
33Mysql
DDoS surge in mobile and data centers
【postgres】postgres 数据库迁移
Illustration Google V8 19: asynchronous programming (II): how does V8 implement async/await?
SSL证书七大常见错误及解决方法
Is online stock trading safe? Do you need to open an account for stock trading?
NCA: the nine year old has launched a DDoS attack
1380. lucky numbers in matrices
DHU programming exercise