-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution155.java
More file actions
48 lines (43 loc) · 1.05 KB
/
Copy pathsolution155.java
File metadata and controls
48 lines (43 loc) · 1.05 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
44
45
46
47
48
import java.util.Stack;
public class solution155 {
private Stack<Integer> stackdata;
private Stack<Integer> stackMin;
public solution155() {
this.stackdata = new Stack<>();
this.stackMin = new Stack<>();
}
public void push(int x) {
if(this.stackMin.isEmpty()){
this.stackMin.push(x);
}else if(x <= this.getMin())
{
this.stackMin.push(x);
}
this.stackdata.push(x);
}
public void pop() {
if(this.stackdata.isEmpty())
{
throw new RuntimeException("1");
}
int value = this.stackdata.pop();
if(value == this.getMin())
{
this.stackMin.pop();
}
}
public int top() {
if(this.stackdata.isEmpty())
{
throw new RuntimeException("2");
}
return this.stackdata.peek();
}
public int getMin() {
if(this.stackMin.isEmpty())
{
throw new RuntimeException("3");
}
return this.stackMin.peek();
}
}