当前位置:网站首页>设计一个有getMin功能的栈
设计一个有getMin功能的栈
2022-06-28 13:46:00 【华为云】
设计一个有getMin功能的栈
【题目】
实现一个特殊的栈,在实现栈的基本功能的基础上,再实现返回栈中最小元素的操作。
【要求】
1.pop、push、getMin操作的时间复杂度都是O(1)。
2.设计的栈类型可以使用现成的栈结构。
【思路】
使用两个栈,一个栈a正常的进出,另外一个栈b用来维护最小值,时刻保持b栈的栈顶是当前a栈中的最小值。
方法一【两栈深度不一致】:入栈时,每次入a栈的时候,如果b栈为空,入a栈同时进入b栈,如果入a栈的值比b栈顶大,则不入,反之,进入b栈,更新最小值;出栈时,如果出栈值等于b栈顶,就把b栈顶也出了,更新最小值,反之,b栈不更新。
举例:依次压入3、4、5、1、2、1的过程中,stackData(a)和stackMin(b)的变化如下图所示。
说明:图片来自左神的程序代码面试指南,仅供学习使用。

方法二【两栈深度保持一致】:入栈时,进入a栈时,如果b栈为空,进入b栈,如果b栈不为空且b栈栈顶小于a栈新入栈的值,那么把b栈的栈顶再次入b栈,高度保持一致;出栈时,两个栈同时弹出。
举例:依次压入3、4、5、1、2、1的过程中,stackData(a)和stackMin(b)的变化如下图所示。

【代码】
【Java】
方法一:
package keafmd.accumulate.codeinterviewguide.stackwithgetmin;import java.util.Stack;/** * Keafmd * * @ClassName: getMinStack * @Description: 有最小值的栈 * @author: 牛哄哄的柯南 * @date: 2022-06-20 14:29 */public class MyStack1 { private Stack<Integer> stacka; private Stack<Integer> stackb; public MyStack1(){ stacka = new Stack<>(); stackb = new Stack<>(); } //入栈 public void push(Integer val){ stacka.push(val); if(stackb.isEmpty()||val<=getMin()){ stackb.push(val); } } //出栈 public Integer pop(){ if(stacka.isEmpty()){ throw new RuntimeException("Stack is empty!"); } Integer val = stacka.pop(); if(val.equals(getMin())){ stackb.pop(); } return val; } //获取最小值 public Integer getMin(){ if(stackb.isEmpty()){ throw new RuntimeException("Stack is empty!"); } return stackb.peek(); }}方法二:
package keafmd.accumulate.codeinterviewguide.stackwithgetmin;import java.util.Stack;/** * Keafmd * * @ClassName: MyStack2 * @Description: 有最小值的栈 * @author: 牛哄哄的柯南 * @date: 2022-06-20 15:07 */public class MyStack2 { private Stack<Integer> stacka; private Stack<Integer> stackb; public MyStack2(){ stacka = new Stack<>(); stackb = new Stack<>(); } //入栈 public void push(Integer val){ stacka.push(val); if(stacka.isEmpty()||val<getMin()){ stackb.push(val); }else{ stackb.push(getMin()); } } //出栈 public Integer pop(){ if(stacka.isEmpty()){ throw new RuntimeException("Stack is empty!"); } Integer val = stacka.pop(); stackb.pop(); return val; } //获取最小值 public Integer getMin(){ if(stackb.isEmpty()){ throw new RuntimeException("Stack is empty!"); } return stackb.peek(); }}【总结】
方法一和方法二其实都是用 stackb 栈保存着 stacka 每一步的最小值。共同点是所有操作的时间复杂度都为O(1)、空间复杂度都为O(n)。区别是:方法一中stackb压入时稍省空间,但是弹出操作稍费时间;方法二中stackb压入时稍费空间,但是弹出操作稍省时间。
边栏推荐
猜你喜欢

Mobile web training day-2

Visual design tutorial of word cloud

3、项目的整体UI架构

木兰开放作品许可证1.0面向社会公开征求意见

Votre Code parle? (1)

为什么越来越多的用户放弃 Swagger,选择Apifox

Pytorch Foundation

Why do more and more users give up swagger and choose apifox

The company leader said that if the personal code exceeds 10 bugs, he will be dismissed. What is the experience?

排序
随机推荐
1015. picking flowers
China Radio and television 5g package is coming, lower than the three major operators, but not as low as expected
G : 最大流问题
木兰开放作品许可证1.0面向社会公开征求意见
众昂矿业着眼氟化工产业,布局新能源产业链
做一个墨水屏电子钟,炫酷!
2021计算机三级数据库大题总结
Inftnews | technology giants accelerate their march into Web3 and metauniverse
i++ , ++i
PostgreSQL超越MySQL
排序
yii2连接websocket服务实现服务端主动推送消息给客户端
Simple understanding of ThreadLocal
欧拉恒等式:数学史上的真正完美公式!
Elastic box auto wrap demo
First knowledge of exception
Hubble database x a joint-stock commercial bank: upgrade the number management system of Guanzi, so that every RMB has an "ID card"
Hundreds of lines of code to implement a JSON parser
How to display the server list of the electric donkey, and how to update the eMule server list
go数组与切片,[]byte转string[通俗易懂]