随便写的,将就看一下吧,实在没搞懂题目想考什么,汗。(以下代码能通过)
public class Solution {
Stack<Integer> stack = new Stack<>();
int min = Integer.MAX_VALUE;
public void push(int node) {
min = Math.min(min, node);
stack.push(node);
}
public void pop() {
int tmp;
if (!stack.isEmpty()) {
tmp = stack.pop();
if (tmp != min) {
return;
}
else {
min = Integer.MAX_VALUE;
Stack<Integer> st = new Stack<>();
while (!stack.isEmpty()) {
tmp = stack.pop();
min = Math.min(min, tmp);
st.push(tmp);
}
while (!st.isEmpty()) {
stack.push(st.pop());
}
}
}
}
public int top() {
int tmp = 0;
if (!stack.isEmpty()) {
tmp = stack.pop();
stack.push(tmp);
}
return tmp;
}
public int min() {
return min;
}
}