当前位置:网站首页>Design a stack with getmin function
Design a stack with getmin function
2022-07-25 07:39:00 【dlz456】
subject : Implement a special stack , On the basis of realizing the basic functions of stack , The operation of the smallest element in the return stack .
requirement :1. pop、push、getMin The time complexity of operation is O(1).
2. The designed stack type can use the existing stack structure
answer : Use two stacks , A stack stackData Is to save all the elements , Another stack stackMin Is to save the minimum value of each step in the current stack . There are two ways to realize it :
The first one is :
Push in data rules :
Put the current number num Put it in stackData When the stack , Judge stackMin Is the stack empty , If it is empty , The current number num Also put stackMin in , If it's not empty , Will judge stackMin Stack top elements and num Size , If num<=stackMin Top element of stack , Just put num Also put stackMin Inside the stack .
Pop up data rules :
Now? stackData The stack pops up the top element num, Judge num and stackMin Whether the elements at the top of the stack are equal . If it's not equal ( The inequality here must be greater than ) Don't worry ; If equal , Just put that stackMin The top element of the stack also pops out .
Query minimum :
stackData The minimum value of is always stackMin The top element of the stack .
Code :
package text11;
import java.util.Stack;
public class MyStack1 {
private Stack<Integer> stackData;
private Stack<Integer> stackMin;
public MyStack1() {
this.stackData=new Stack<Integer>();
this.stackMin=new Stack<Integer>();
}
public void push(int newNum) {
if (this.stackMin.isEmpty()) {
this.stackMin.push(newNum);
}else if(newNum<=this.getmin()) {
this.stackMin.push(newNum);
}
this.stackData.push(newNum);
}
public int pop() {
if (this.stackData.isEmpty()) {
throw new RuntimeException("Your stack is empty.");
}
int value=this.stackData.pop();
if(value==this.getmin()) {
this.stackMin.pop();
}
return value;
}
public int getmin() {
if(this.stackMin.empty()){
throw new RuntimeException("Your stack is empty.");
}
return this.stackMin.peek();
}
}The second kind :
Push in data rules :
Put the current number num Put it in stackData When the stack , Judge stackMin Is the stack empty , If it is empty , The current number num Also put stackMin in , If it's not empty , Will judge stackMin Stack top elements and num Size , If num<=stackMin To the top of the stack Elements , Just put num Also put stackMin Inside the stack . otherwise , take stackMin Stack top element re pressing stackMin In the stack .
Pop up data rules :
Pop up a stackData Stack element , At the same time, a stackMin Stack element
Query minimum :
stackData The minimum value of is always stackMin The top element of the stack .
Code :
package text11;
import java.util.Stack;
public class MyStack2 {
private Stack<Integer> stackData;
private Stack<Integer> stackMin;
public MyStack2() {
this.stackData=new Stack<Integer>();
this.stackMin=new Stack<Integer>();
}
public void push(int newNum) {
if (this.stackMin.isEmpty()) {
this.stackMin.push(newNum);
}else if(newNum<=this.getmin()) {
this.stackMin.push(newNum);
}else {
int value1=getmin();
this.stackMin.push(value1);
}
this.stackData.push(newNum);
}
public int pop() {
if (this.stackData.isEmpty()) {
throw new RuntimeException("Your stack is empty.");
}
int value=this.stackData.pop();
this.stackMin.pop();
return value;
}
public int getmin() {
if(this.stackMin.empty()){
throw new RuntimeException("Your stack is empty.");
}
return this.stackMin.peek();
}
}
边栏推荐
- [software testing] package resume from these points to improve the pass rate
- QT学习日记20——飞机大战项目
- A fast method of data set enhancement for deep learning
- Design of workflow system
- Xinku online | cnopendata shareholder information data of A-share listed companies
- [unity introduction plan] interface Introduction (2) -games view & hierarchy & Project & Inspector
- P1086 [NOIP2004 普及组第二题] 花生采摘
- 【Unity入门计划】基本概念-预制件 Prefab
- [wechat applet] global style, local style, global configuration
- Nailing the latest version, how to clear the login phone number history data
猜你喜欢

Before Oracle 19C migration, how important is it to do a good job of rat playback test?

9 best engineering construction project management systems

【Unity入门计划】基本概念-2D碰撞体Collider 2D

RPC communication principle and project technology selection

cesium简介

NLP hotspots from ACL 2022 onsite experience

What has become a difficult problem for most people to change careers, so why do many people study software testing?

Pads export Gerber file

nanodet训练时出现问题:ModuleNotFoundError: No module named ‘nanodet‘的解决方法
![[unity introduction program] basic concept - preform prefab](/img/c6/aac7bffdf99073978f9b2f8ff15fbb.png)
[unity introduction program] basic concept - preform prefab
随机推荐
Problems in deep learning training and testing: error: the following arguments are required: --dataroot, solution: the configuration method of training files and test files
【Unity入门计划】基本概念-触发器 Trigger
Tunnel broadcasting and wireless trunking communication broadcasting system - the case of Tiantaishan tunnel
轮询、中断、DMA和通道
[recommended reading] a collection of super useful vulnerability scanning tools!
How to use network installation to deploy multiple virtual servers in KVM environment
10 key points and 5 measures for good project management
Nailing the latest version, how to clear the login phone number history data
Pads export Gerber file
[paper notes] progressive layered extraction (PLE): a novel multi task learning (MTL) model for personalized
钉钉最新版,怎么清除登录手机号历史记录数据
RPC communication principle and project technology selection
【PyTorch】最常见的view的作用
【软件测试】包装简历从这几点出发、提升通过率
【论文笔记】Progressive Layered Extraction (PLE): A Novel Multi-Task Learning (MTL) Model for Personalized
List derivation
New functions of shixizhi are online. These new functions are online in June. Can you use them?
新库上线| CnOpenDataA股上市公司股东信息数据
[unity introduction program] basic concepts -2d rigid body 2D
Practical operation: elegant downtime under large-scale micro service architecture