class MinStack {
Deque<StackNode> stack;
/** initialize your data structure here. */
public MinStack() {
stack = new ArrayDeque<>();
}
public void push(int x) {
StackNode node;
if (stack.isEmpty()) {
node = new StackNode(x, x);
} else {
node = new StackNode(x, Math.min(x,stack.peek().getMinVal()));
}
stack.push(node);
}
public void pop() {
stack.pop();
}
public int top() {
return stack.peek().getCurrentVal();
}
public int min() {
return stack.peek().getMinVal();
}
}
class StackNode {
Integer currentVal;
Integer minVal;
public StackNode(Integer currentVal, Integer minVal) {
this.currentVal = currentVal;
this.minVal = minVal;
}
public Integer getCurrentVal() {
return currentVal;
}
public void setCurrentVal(Integer currentVal) {
this.currentVal = currentVal;
}
public Integer getMinVal() {
return minVal;
}
public void setMinVal(Integer minVal) {
this.minVal = minVal;
}
}
/**
* Your MinStack object will be instantiated and called as such:
* MinStack obj = new MinStack();
* obj.push(x);
* obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.min();
*/
评论