PriorityQueue的使用
java 的PriorityQueue 以最小堆为默认方式,而c++默认方式是最大堆。
import java.util.*; public class Jesse{ public static void main(String[] args) { Queue<Integer> que=new PriorityQueue<>(10, new Comparator<Integer>() { @Override public int compare(Integer o1, Integer o2) { //return o1-o2;//最小堆 默认的情况 return o2-o1;//最大堆 } }); que.offer(5); que.offer(1); que.offer(9); System.out.println(que.size()); System.out.println(que.isEmpty()); System.out.println(que.poll()); System.out.println(que.poll()); System.out.println(que.poll()); System.out.println(que.isEmpty()); } }

浙公网安备 33010602011771号