PriorityQueue基本用法

PriorityQueue<Integer> heap = new PriorityQueue<>();

// 基本操作
heap.add(1);
heap.poll();
heap.peek();
heap.clear();
heap.contains(1);
heap.size();

// 默认为小顶堆,大顶堆后减前
PriorityQueue<Integer> heap_desc = new PriorityQueue<>((a, b)->b-a);

// 自定义非常规数据结构的排序规则:字符串长度降序排列
PriorityQueue<Integer> heap_strlen_desc = new PriorityQueue<>(new Comparator<String>() {
    @Override
    public int compare(String o1, String o2) {
        return o2.length()-o1.length();
    }
});

经典题目:合并K个升序链表 https://leetcode.cn/problems/merge-k-sorted-lists/description/

posted @ 2023-03-03 22:08  zhongyu_127  阅读(32)  评论(0)    收藏  举报