当前位置:网站首页>[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();}} 边栏推荐
猜你喜欢

22.支持向量机—高斯核函数

security CSRF漏洞保护

Excel导入和导出

不了解SynchronousQueue?那ArrayBlockingQueue和LinkedBlockingQueue不会也不知道吧?

Wincc报表教程(SQL数据库的建立,wincc在数据库中保存和查询数据,调用Excel模板把数据保存到指定的位置和打印功能)

Detailed explanation of Zadig's self-testing and tuning environment technical solution for developers

在MySQL中使用MD5加密【入门体验】

带你搞懂MySQL隔离级别,两个事务同时操作同一行数据会怎样?

Enterprise firewall management, what firewall management tools are there?

Ansible中的任务执行控制
随机推荐
接地气讲解TCP协议和网络程序设计
不就是个TCC分布式事务,有那么难吗?
Axure tutorial - the new base (small white strongly recommended!!!)
【Leetcode】470. Implement Rand10() Using Rand7()
Architecture basic concept and nature of architecture
yay 报错 response decoding failed: invalid character ‘<‘ looking for beginning of value;
[Three sons] C language implements simple three sons
多御安全浏览器android版更新至1.7,改进加密协议
重装腾讯云云监控后如果对应服务不存在可通过sc.exe命令添加服务
当奈飞的NFT忘记了Web2的业务安全
Deliver cloud-native microservices applications with Zadig
IP核:FIFO
一篇永久摆脱Mysql时区错误问题,idea数据库可视化插件配置
Thinkphp 5.0.24变量覆盖漏洞导致RCE分析
LeetCode_322_零钱兑换
Bean的生命周期
CRS 管理与维护
20220725 Information update
Axure教程-新手入门基础(小白强烈推荐!!!)
Share an interface test project (very worth practicing)