1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| class Solution { public int[][] kClosest(int[][] points, int K) { PriorityQueue<int[]> queue = new PriorityQueue<>((o1, o2) -> { Double dist1 = Math.sqrt(o1[0]*o1[0] + o1[1]*o1[1]); Double dist2 = Math.sqrt(o2[0]*o2[0] + o2[1]*o2[1]); return dist1.compareTo(dist2); });
for(int[] arr : points){ queue.offer(arr); }
int[][] ans = new int[K][2]; int i = 0; while (K > 0) { int[] tmp = queue.poll(); ans[i++] = tmp; K--; } return ans; } }
|