题解 | #包含min函数的栈#

包含min函数的栈

http://www.nowcoder.com/practice/4c776177d2c04c2494f2555c9fcc1e49


public class Solution {
    //要求时间复杂度为o(1),使用辅助栈,存储每次的最小值
    Stack<Integer> minStack=new Stack<>();
    Stack<Integer> min=new Stack<>();
    
    public void push(int node) {
        if(min.empty()) min.push(node);
        else min.push(Math.min(min.peek(),node));
        minStack.push(node);
    }
    
    public void pop() {
        minStack.pop();
        min.pop();
    }
    
    public int top() {
        return minStack.peek();
    }
    
    public int min() {
        return min.peek();
    }
}
全部评论

相关推荐

03-31 21:47
东南大学 C++
彭于晏前来求offe...:吓晕了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务