包含Min函数的stack

题目

题解

import java.util.Stack;

public class Solution {

    Stack<Integer> stack = new Stack();
    Stack<Integer> helpStack = new Stack();
    public void push(int node) {
        int helpStackTop = node;
        if (!helpStack.isEmpty()) {
            helpStackTop = Math.min(helpStack.peek(), helpStackTop);
        }
        stack.push(node);
        helpStack.push(helpStackTop);
    }
    
    public void pop() {
        if (stack.isEmpty()) {
            return;
        }
        stack.pop();
        helpStack.pop();
    }
    
    public int top() {
        return stack.peek();
    }
    
    public int min() {
        return helpStack.peek();
    }
}

《包含Min函数的stack》

    原文作者:Acceml
    原文地址: https://segmentfault.com/a/1190000017407885
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞