首页 > 其他 > 详细

牛客(20)包含min函数的栈

时间:2018-05-08 13:36:46      阅读:114      评论:0      收藏:0      [点我收藏+]
    //
//    题目描述
//    定义栈的数据结构,请在该类型中实现一个能够得到栈最小元素的min函数。
    Stack<Integer> stack = new Stack<Integer>();
    Stack<Integer> stackMin = new Stack<Integer>();

    public void push(int node) {
        stack.push(node);
        if (stackMin.isEmpty()){
            stackMin.push(node);
        }else {
            if (node<stackMin.peek()){
                stackMin.push(node);
            }
        }
    }

    public void pop() {
        if(stack.pop()==stackMin.peek()){
         stackMin.pop();
        }
    }

    public int top() {
        return stack.peek();
    }

    public int min() {
        return stackMin.peek();
    }

 

牛客(20)包含min函数的栈

原文:https://www.cnblogs.com/kaibing/p/9007569.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!