当前位置:网站首页>[Headline] Written test questions - minimum stack
[Headline] Written test questions - minimum stack
2022-08-02 00:15:00 【Chen Yikang】


Solution 1: (relative to solution 2, it will be very tasteless)
Thinking:
- Because the time complexity of obtaining the minimum element of the stack is O(1), all use a variable min to store the minimum value
- Set the variable min that stores the minimum value to Integer.MAX_VALUE
- In the push method, use min to compare each push
- Create two stacks, the first stack is used to store data, and the second stack is used to obtain the zero-hour stack with the minimum data through the first stack of variables
class MinStack {public Stack stack;public Stack stackTmp;public MinStack() {stack = new Stack<>();stackTmp = new Stack<>();}// first assign the maximum valuepublic int min = Integer.MAX_VALUE;public void push(int val) {// Assign a value if it is smaller than himif(val < min){min = val;}stack.push(val);}public void pop() {if(stack.pop() == min){//min needs to be reassigned after being popped, still using the maximum valuemin = Integer.MAX_VALUE;//Transfer all elements to stackTmp, the purpose is to make min re-comparison with all elementswhile(!stack.empty()){int tmp = stack.pop();if(tmp < min){min = tmp;}stackTmp.push(tmp);}// then move to stackwhile(!stackTmp.empty()){stack.push(stackTmp.pop());}}}public int top() {return stack.peek();}public int getMin() {return min;}} Solution 2: (analysis is as follows)





The code is as follows:
class MinStack {Stack stack;//Normal stackStack stackMin;//Minimum stackpublic MinStack() {stack = new Stack<>();stackMin = new Stack<>();}// push stackpublic void push(int val) {//Assign the initial value to the minimum stationif(stackMin.empty()){stackMin.push(val);}//If it is not the initial value, first judge whether it is less than or equal to (to prevent it from being empty after pop) the top element of the minimum stack, if so, push the stack to the minimum stackelse if(stackMin.peek() >= val){stackMin.push(val);}stack.push(val);}public void pop() {if((int)stackMin.peek() == (int)stack.peek()){stackMin.pop();}stack.pop();}public int top() {return stack.peek();}public int getMin() {return stackMin.peek();}} 边栏推荐
- 08-SDRAM:汇总
- 【Leetcode】478. Generate Random Point in a Circle(配数学证明)
- 【Leetcode】475. Heaters
- 【Leetcode】473. Matchsticks to Square
- Architecture basic concept and nature of architecture
- 接地气讲解TCP协议和网络程序设计
- 【解决】win10下emqx启动报错Unable to load emulator DLL、node.db_role = EMQX_NODE__DB_ROLE = core
- easy-excel 解决百万数据导入导出,性能很强
- 控制电机的几种控制电路原理图
- solidity
猜你喜欢
随机推荐
LeetCode_322_零钱兑换
TexturePacker使用文档
06-SDRAM :SDRAM控制模块
Using the "stack" fast computing -- reverse polish expression
Axure教程-新手入门基础(小白强烈推荐!!!)
[Three sons] C language implements simple three sons
字节跳动面试官:请你实现一个大文件上传和断点续传
使用 Zadig 交付云原生微服务应用
C语言七夕来袭!是时候展现专属于程序员的浪漫了!
els 方块边界变形处理
Chrome书签插件,让你实现高效整理
DVWA靶场环境搭建
如何进行数据库备份
尚硅谷MySQL学习笔记
不了解SynchronousQueue?那ArrayBlockingQueue和LinkedBlockingQueue不会也不知道吧?
07-SDRAM :FIFO控制模块
How to reinstall Win11?One-click method to reinstall Win11
短视频seo搜索优化主要内容
Docker实践经验:Docker 上部署 mysql8 主从复制
【Leetcode】475. Heaters









