关于力扣LeetCode第155题最小栈

看题目还以为是要让自己设计一个栈的结构,结果官方题解里就是直接用的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();
        }
    }


 

猜你喜欢

转载自blog.csdn.net/weixin_43967331/article/details/106129877