码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-05-08 14:46:30      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:node   包含   元素   类型   empty   stack   push   ==   col   

    //
//    题目描述
//    定义栈的数据结构,请在该类型中实现一个能够得到栈最小元素的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函数的栈

标签:node   包含   元素   类型   empty   stack   push   ==   col   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!