当前位置:网站首页>设计一个有getMin功能的栈
设计一个有getMin功能的栈
2022-07-25 07:38:00 【dlz456】
题目:实现一个特殊的栈,在实现栈的基本功能的基础上,再实现返回栈中最小元素的操作。
要求:1. pop、push、getMin操作的时间复杂度都是O(1)。
2. 设计的栈类型可以使用现成的栈结构
解答:使用两个栈,一个栈stackData就是保存所有的元素,另一个栈stackMin就是保存当前栈中每一步的最小值。具体实现方式有两种:
第一种:
压入数据规则:
将当前的数num放到stackData栈的时候,判断stackMin栈是否为空,如果为空,就将当前数num也放到stackMin中,如果不为空,就判断stackMin栈顶元素和num的大小,如果num<=stackMin栈顶元素,就把num也放到stackMin栈里面。
弹出数据规则:
现在stackData栈弹出栈顶元素num,判断num和stackMin栈顶元素是否相等。如果不相等(这里的不相等肯定是大于)就不用管;如果相等,就把那个stackMin栈顶元素也弹出来。
查询最小值:
stackData的最小值始终是stackMin栈的栈顶元素。
代码:
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();
}
}第二种:
压入数据规则:
将当前的数num放到stackData栈的时候,判断stackMin栈是否为空,如果为空,就将当前数num也放到stackMin中,如果不为空,就判断stackMin栈顶元素和num的大小,如果num<=stackMin 栈顶 元素,就把num也放到stackMin栈里面。否则,将stackMin栈顶元素再压入stackMin栈中。
弹出数据规则:
弹出一个stackData栈元素,同时也弹出一个stackMin栈元素
查询最小值:
stackData的最小值始终是stackMin栈的栈顶元素。
代码:
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();
}
}
边栏推荐
- When importing data in batches, you always prompt "failure reason: SQL parsing failure: parsing file failure:: null". What's the matter?
- 【Unity入门计划】基本概念-2D刚体Rigidbody 2D
- QT学习日记20——飞机大战项目
- 深度学习之快速实现数据集增强的方法
- How to use network installation to deploy multiple virtual servers in KVM environment
- 【Unity入门计划】基本概念-触发器 Trigger
- Polling, interrupt, DMA and channel
- UXDB怎么从日期值中提取时分秒?
- Matlab self programming series (1) -- angular distribution function
- 【微信小程序】全局样式、局部样式、全局配置
猜你喜欢

Use cyclegan to train self-made data sets, popular tutorials, and get started quickly

华为无线设备配置WPA2-802.1X-AES安全策略

Analysis of difficulties in diagramscene project

Oracle19 adopts automatic memory management. The AWR report shows that the settings of SGA and PGA are too small?

【Unity入门计划】基本概念-预制件 Prefab
![[software testing] package resume from these points to improve the pass rate](/img/69/b27255c303150430df467ff3b5cd08.gif)
[software testing] package resume from these points to improve the pass rate

JS cannot get content disposition in headers

Have you got the advanced usage of pytest?

Robot framework mobile terminal Automation Test ----- 01 environment installation

MATLAB自编程系列(1)---角分布函数
随机推荐
Huawei wireless device configuration wpa2-802.1x-aes security policy
【Unity入门计划】界面介绍(2)-Games视图&Hierarchy&Project&Inspector
A fast method of data set enhancement for deep learning
Offline base tile, which can be used for cesium loading
做好项目管理的10个关键点和5大措施
How does uxdb extract hours, minutes and seconds from date values?
Beijing internal promotion | Microsoft STCA recruits nlp/ir/dl research interns (remote)
大佬秋招面经
toolbar的使用
2-6. Automatic acquisition
Google Earth engine - Landsat 1985-2020 ecological remote sensing index resi calculation
Load capacity - sorting out the mind map that affects load capacity
【Unity入门计划】界面介绍(1)-Scene视图
3. Promise
Bingbing's learning notes: classes and objects (Part 1)
P1086 [NOIP2004 普及组第二题] 花生采摘
[dynamic programming] - Knapsack model
JS cannot get content disposition in headers
Lidar construction map (overlay grid construction map)
【软件测试】包装简历从这几点出发、提升通过率