import java.util.Stack;
public class Solution {
//stack1用来保存普通元素
Stack<Integer> stack1 = new Stack<>();
//stack2用来保存最小的元素,放进去的元素只能比下面的小
Stack<Integer> stack2 = new Stack<>();
public void push(int node) {
stack1.push(node);
//如果stack2为空直接放
if(stack2.isEmpty()) {
stack2.push(node);
}else{
//不为空先比较一下下面的
int top = stack2.peek();
if(node <= top) {
stack2.push(node);
}else{
//为了保证两个栈的数量一致
stack2.push(top);
}
}
}
public void pop() {
if(!stack1.isEmpty()) {
stack1.pop();
}
if(!stack2.isEmpty()) {
stack2.pop();
}
}
public int top() {
return stack1.peek();
}
public int min() {
return stack2.peek();
}
}
01-11
189
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)