当前位置:网站首页>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();
}
}
边栏推荐
- Xinku online | cnopendata shareholder information data of A-share listed companies
- [paper notes] progressive layered extraction (PLE): a novel multi task learning (MTL) model for personalized
- 【论文笔记】Next-ViT: Next Generation Vision Transformer for Efficient Deployment in Realistic Industrial
- [unity entry program] make my first little game
- Beijing internal promotion | Microsoft STCA recruits nlp/ir/dl research interns (remote)
- What are the types of financial products in 2022? Which is suitable for beginners?
- 3. Promise
- 【论文笔记】EFFICIENT CNN ARCHITECTURE DESIGN GUIDED BY VISUALIZATION
- Oracle19采用自动内存管理,AWR报告显示SGA、PGA设置的过小了?
- Practical operation: elegant downtime under large-scale micro service architecture
猜你喜欢
随机推荐
MATLAB自编程系列(1)---角分布函数
Ask the bosses: MySQL CDC stores configuration data, and Kafka has history
【ES6】函数的参数、Symbol数据类型、迭代器与生成器
P1049 [NOIP2001 普及组 T4] 装箱问题
【Unity入门计划】基本概念-2D碰撞体Collider 2D
Design of workflow system
Introduction to Manhattan distance
3. Promise
Nailing the latest version, how to clear the login phone number history data
How should enterprise users choose aiops or APM?
Summer Challenge harmonyos - slider slider for custom components
Huawei wireless device configuration wpa2-802.1x-aes security policy
RPC communication principle and project technology selection
Use of toolbar
JS note 17: the whole process of jest project configuration of typescript project
【程序员2公务员】三、资源搜集
toolbar的使用
J1 常用的DOS命令(P25)
Practical skills -- some solutions to small problems
深度学习训练和测试时出现问题:error: the following arguments are required: --dataroot,解决:训练文件的配置方法和测试文件的配置方法







![[unity entry program] basic concept trigger](/img/16/cd0f8ae579627fc095935195136729.png)
