LeetCode 155 - 最小栈 - [数组模拟栈]
2024-08-25 14:04:24
题目链接:https://leetcode-cn.com/problems/min-stack/description/
设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈。
push(x) -- 将元素 x 推入栈中。
pop() -- 删除栈顶的元素。
top() -- 获取栈顶元素。
getMin() -- 检索栈中的最小元素。
题解:
以往,我们经常用 $min[i]$ 维护区间 $[1,i]$ 的最小值,在这里我们同样也可以这么做。
AC代码(要想超过百分百的cpp提交记录,关闭IO同步是必不可少的……):
static const auto io_sync_off = []()
{
// turn off sync
std::ios::sync_with_stdio(false);
// untie in/out streams
std::cin.tie(nullptr);
return nullptr;
}();
#define maxn 10000
struct MinStack
{
int s[maxn],mn[maxn],tot;
MinStack() {
tot=;
}
void push(int x) {
s[tot]=x;
if(tot) mn[tot]=min(mn[tot-],x);
else mn[tot]=x;
tot++;
}
void pop() {
tot--;
}
int top() {
return s[tot-];
}
int getMin() {
return mn[tot-];
}
};
最新文章
- android持久化技术
- 检测cpu是否支持虚拟化和二级地址转换【转】
- java.lang.ClassCastException: com.sun.proxy.$Proxy32 cannot be cast to com.bkc.bpmp.core.cache.MemcachedManager
- Android View坐标getLeft, getRight, getTop, getBottom
- [转]CharacterController与Rigidbody
- (整理)ubuntu 的 相关知识(来自 鸟哥的私房菜)
- Docker存储驱动之AUFS简介
- x&;(x-1)
- MySql解除安全模式:Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column.
- pyinstaller
- Ubuntu下使用QQ/Wechat
- oracle的DBMS_JOB相关知识
- 在VS2017上使用C#调用非托管C++生成的DLL文件(图文讲解)
- Python基础-day02
- linux下修改tomcat使用的jdk版本
- YQCB冲刺第二周第二天
- linux下mysql 启动命令
- 创建ASM实例及ASM数据库(转载)
- 【Linux】cd命令
- Python基础:文件的基本操作