思路:
两个栈,栈1压入元素,栈2存当前最小元素,
栈2为空时,压入当前元素,栈2不空时,比较栈2栈顶元素和压入元素大小,压入元素小,则把压入元素压入到栈2中,否则不压入。
import java.util.Stack;
public class Solution {
Stack<Integer> stack1=new Stack<>();
Stack<Integer> stack2=new Stack<>();
public void push(int node) {
stack1.push(node);
if(stack2.isEmpty())
{
stack2.push(node);
}
else{
if(node<stack2.peek())
{
stack2.push(node);
}
}
}
public void pop() {
if(stack1.pop()==stack2.peek())
stack2.pop();
}
public int top() {
return stack1.peek();
}
public int min() {
return stack2.peek();
}
}