-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_155_min_stack.java
More file actions
48 lines (39 loc) · 1.07 KB
/
Copy path_155_min_stack.java
File metadata and controls
48 lines (39 loc) · 1.07 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 _155_min_stack {
class MinStack {
class Node {
int value;
int MIN;
Node(int value) { this.value = value; }
}
Stack<Node> myStack = new Stack<>();
public MinStack() {
}
public void push(int val) {
Node newNode = new Node(val);
if (myStack.isEmpty()) {
newNode.MIN = newNode.value;
} else {
newNode.MIN = Math.min(newNode.value, myStack.peek().MIN);
}
myStack.push(newNode);
}
public void pop() {
if (!myStack.isEmpty()) {
myStack.pop();
}
}
public int top() {
if (!myStack.isEmpty()) {
return myStack.peek().value;
}
return -1;
}
public int getMin() {
if (!myStack.isEmpty()) {
return myStack.peek().MIN;
}
return -1;
}
}
}