int points[][] = { { 0, 1 }, { 1, 0 }, {3,3},{5,-1},{-2,4}}, k = 2;
Queue<int[]> pq = new PriorityQueue<>((a, b) -> ((b[0] * b[0] + b[1] * b[1]) - (a[0] * a[0] + a[1] * a[1])));
for (int[] e : points) {
pq.add(e);`
if (pq.size() > k)
pq.remove();
}
有人可以向我解释此代码的工作原理吗?感谢您的帮助
使用PriorityQueue排序int []的代码,仅选择top2。比较器功能是:
我从不需要使用PriorityQueue
,所以我不能权威地评论上述达到期望目标的方法。