设计一个有getMin功能的栈

【题目】

  实现一个特殊的栈,在实现栈的基本功能的基础上,再实现返回栈中的最小元素的操作。

【要求】

  1.pop、push、getMin操作的时间复杂度都是O(1)。

  2.设计的栈类型可以使用现成的栈结构。

方案一的代码实现如下:

复制代码
 1 import java.util.Stack;
 2 public class MyStack1 {
 3     private Stack<Integer> stackData;
 4     private Stack<Integer> stackMin;
 5     public MyStack1()
 6     {
 7         this.stackData=new Stack<Integer>();
 8         this.stackMin=new Stack<Integer>();
 9     }
10     public void push(int newNum)
11     {
12         if(this.stackMin.isEmpty()||newNum<=this.getmin())
13             this.stackMin.push(newNum);
14         this.stackData.push(newNum);
15     }
16     public int pop()
17     {
18         if(this.stackData.isEmpty())
19             System.out.println("Your stack is empty!");
20         int value=this.stackData.pop();
21         if(value==this.getmin())
22             this.stackMin.pop();
23         return value;
24     }
25     public int getmin()
26     {
27         if(this.stackMin.isEmpty())
28             System.out.println("Your stack is empty!");
29         return this.stackMin.peek();
30     }
31     public static void main(String[] args)
32     {
33         MyStack1 s=new MyStack1();
34         s.push(3);
35         s.push(4);
36         s.push(5);
37         s.push(1);
38         s.push(2);
39         s.push(1);
40         System.out.println(s.getmin());
41         System.out.println(s.pop());
42         System.out.println(s.pop());
43         System.out.println(s.pop());
44         System.out.println(s.pop());
45         System.out.println(s.pop());
46         System.out.println(s.pop());
47         System.out.println(s.pop());
48     }
49 }
复制代码

运行结果如下:

1
1
2
1
5
4
3
Your stack is empty!

方法二的实现代码如下:

复制代码
 1 import java.util.Stack;
 2 public class MyStack2 {
 3     private Stack<Integer> stackData;
 4     private Stack<Integer> stackMin;
 5     public MyStack2()
 6     {
 7         this.stackData=new Stack<Integer>();
 8         this.stackMin=new Stack<Integer>();
 9     }
10     public void push(int newNum)
11     {
12         if(this.stackMin.isEmpty()||newNum<this.getmin())
13             this.stackMin.push(newNum);
14         else
15         {
16             int newMin=this.stackMin.peek();
17             this.stackMin.push(newMin);
18         }
19         this.stackData.push(newNum);
20     }
21     public int pop()
22     {
23         if(this.stackData.isEmpty())
24             System.out.println("Your stack is empty!");
25         this.stackMin.pop();
26         return this.stackData.pop();
27     }
28     public int getmin()
29     {
30         if(this.stackMin.isEmpty())
31             System.out.println("Your stack is empty!");
32         return this.stackMin.peek();
33     }
34     public static void main(String[] args)
35     {
36         MyStack2 s=new MyStack2();
37         s.push(3);
38         s.push(4);
39         s.push(5);
40         s.push(1);
41         s.push(2);
42         s.push(1);
43         System.out.println(s.getmin());
44         System.out.println(s.pop());
45         System.out.println(s.pop());
46         System.out.println(s.pop());
47         System.out.println(s.pop());
48         System.out.println(s.pop());
49         System.out.println(s.pop());
50         System.out.println(s.pop());
51     }
52 }
复制代码

运行结果如下:

1
1
2
1
5
4
3
Your stack is empty!

 

posted @   Angel_Kitty  阅读(655)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述
点击右上角即可分享
微信分享提示
哥伦布
14°
14:09发布
哥伦布
14:09发布
14°
大雨
南风
3级
空气质量
相对湿度
93%
今天
中雨
14°/19°
周日
中雨
5°/19°
周一
1°/11°