大顶堆和小顶堆模版

大顶堆和小顶堆模版

//特殊情况可能出错:例如 Integer.MIN_VALUE - Integer.MAX_VALUE = 1
//大顶堆
PriorityQueue<Integer> maxHeapQueue = new PriorityQueue<>((o1, o2) -> o2 - o1);
//小顶堆
PriorityQueue<Integer> minHeapQueue = new PriorityQueue<>((o1, o2) -> o1 - o2);

PriorityQueue<Integer> maxHeapQueue1 = new PriorityQueue<>((o1, o2) -> {
    if (o2 > o1) {
        return 1;
    } else if (o2 == o1) {
        return 0;
    } else {
        return -1;
    }
});

PriorityQueue<Integer> minHeapQueue1 = new PriorityQueue<>((o1, o2) -> {
    if (o1 > o2) {
        return 1;
    } else if (o1 == o2) {
        return 0;
    } else {
        return -1;
    }
});
原文地址:https://www.cnblogs.com/zhihaospace/p/12760158.html