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

包含min函数的栈

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

import java.util.Stack;
import java.util.PriorityQueue;

public class Solution {


    PriorityQueue<Integer> priorityQueue = new PriorityQueue<Integer>();
    Stack<Integer> stack = new Stack<Integer>();

    public void push(int node) {
        stack.push(node);
        priorityQueue.add(node);
    }

    public void pop() {
        priorityQueue.remove(stack.peek());
        stack.pop();
    }

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

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

全部评论

相关推荐

脾气小祖宗:这简历摸到都得狠狠地消毒液洗手😂
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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