数组模拟一个栈(push,pop和max_valcaozuo)
介绍
这个数组模拟的栈拥有push、pop还有找到当前栈的最大值的操作。找到最大值的操作时间复杂度为o(1)。
主要就是push的时候会和前面的最大值比较,然后数组存储最大值,这里因为不用获取top所以没有用top函数。
具体应用题目链接
源码
class STACK
{
public:
int cnt = 0;
int f[100000] = {};
void push(int x)
{
cnt ++;
f[cnt] = max(f[cnt - 1], x);
}
void pop()
{
if (cnt != 0) cnt --;
}
int max_val()
{
return f[cnt];
}
};
版权声明:
作者:Reid
链接:https://www.ricemoon.cn/algorithm/tmplate/105.html
来源:RiceMoon
文章版权归作者所有,未经允许请勿转载。
THE END
0
二维码
海报
数组模拟一个栈(push,pop和max_valcaozuo)
数组模拟一个栈(push,pop和max_valcaozuo)

共有 0 条评论