当前位置:网站首页>设计一个有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压入时稍费空间,但是弹出操作稍省时间。
边栏推荐
猜你喜欢
Design artificial intelligence products: technical possibility, user acceptability and commercial feasibility
thinkphp6 多级控制器目录访问解决方法
其他国产手机未能填补华为的空缺,苹果在高端手机市场已无对手
公司领导说,个人代码超10个Bug就开除,是什么体验?
Inftnews | technology giants accelerate their march into Web3 and metauniverse
Other domestic mobile phones failed to fill the vacancy of Huawei, and apple has no rival in the high-end mobile phone market
New product experience: Alibaba cloud's new generation of local SSD instance I4 open beta
Kubernetes in-depth understanding of kubernetes (I)
Solution to directory access of thinkphp6 multi-level controller
PostgreSQL surpasses MySQL
随机推荐
RK3399平台开发系列讲解(使用篇)Pinctrl子系统的介绍 - 视频介绍
(原创)【MAUI】一步一步实现“悬浮操作按钮”(FAB,Floating Action Button)
If a programmer goes to prison, will he be assigned to write code?
Forecast and Analysis on market scale and development trend of China's operation and maintenance security products in 2022
To be the Italian Islander? Liuqiangdong cashed out 6.6 billion yuan in two months and made a one-time 560million "emergency transfer" to buy the European maritime Palace
Analysis and processing of GPS data format [easy to understand]
《蛤蟆先生去看心里医生》阅读笔记
设计人工智能产品:技术可能性、用户合意性、商业可行性
Why will the new 5g standard bring lower TCO to the technology stack
NPOI导出Excel并下载到客户端
Pytorch model
[codec] write H264 decoder (1) from scratch
公司领导说,个人代码超10个Bug就开除,是什么体验?
Recognize the startup function and find the user entry
APP冷热启动专项测试
华泰证券开户怎么开 怎么办理开户最安全
CVPR再起争议:IBM中稿论文被指照搬自己承办竞赛第二名的idea
MySQL多表联合查询
Rk3399 platform development series explanation (use part) pinctrl subsystem introduction - Video Introduction
go数组与切片,[]byte转string[通俗易懂]