import java.util.Stack;
public class GetMinStack {
public static class MyStack {
public Stack<Integer> stackData;
public Stack<Integer> stackMin;
public MyStack() {
this.stackData = new Stack<Integer>();
this.stackMin = new Stack<Integer>();
}
public void push(int value) {
if (stackMin.isEmpty()) {
stackMin.push(value);
} else if (value < stackMin.peek()) {
stackMin.push(value);
} else {
stackMin.push(stackMin.peek());
}
stackData.push(value);
}
public int pop() {
if (stackData.isEmpty()) {
throw new RuntimeException("当前栈为空");
}
stackMin.pop();
return stackData.pop();
}
public int getMin() {
if (stackMin.isEmpty()) {
throw new RuntimeException("当前栈为空");
}
return stackMin.peek();
}
}
public static void main(String[] args) {
MyStack stack = new MyStack();
stack.push(3);
stack.push(0);
System.out.println(stack.getMin());
stack.pop();
System.out.println(stack.getMin());
}
}