看题目还以为是要让自己设计一个栈的结构,结果官方题解里就是直接用的Stack。
我这里用链表实现了,输出正常,就是getMin方法可以优化一下,暂时这样了
class MinStack {
private final List<Integer> valueList;
private int nowTopLoction;
public MinStack() {
this.valueList=new LinkedList<Integer>();
this.nowTopLoction=-1;
}
public void push(int x) {
this.valueList.add(x);
this.nowTopLoction++;
}
public void pop() {
this.valueList.remove(this.nowTopLoction);
this.nowTopLoction--;
}
public int top() {
return this.valueList.get(nowTopLoction);
}
public int getMin() {
return this.valueList.stream().min(Integer::compareTo).get();
}
}