-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyStack1.java
More file actions
43 lines (37 loc) · 1.11 KB
/
MyStack1.java
File metadata and controls
43 lines (37 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package algorithm.stack;
import java.util.Stack;
/**
* @Author: ChangXuan
* @Decription: 实现一个特殊的栈,在实现栈的基本功能的基础上,再实现返回栈中最小元素的操作
* @Date: 12:45 2020/6/19
**/
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(Integer newNum){
this.stackData.push(newNum);
if (this.stackMin.isEmpty() || newNum <= this.getMin()){
this.stackMin.push(newNum);
}
}
public Integer pop(){
if (this.stackData.isEmpty()){
throw new RuntimeException("Stack is empty");
}
Integer value = this.stackData.pop();
if (value.equals(this.getMin())){
this.stackMin.pop();
}
return value;
}
public Integer getMin(){
if (this.stackMin.isEmpty()){
throw new RuntimeException("Yous stack is empty");
}
return this.stackMin.peek();
}
}