一定要早日上岸鸭 · August 2, 2021 0

973. 最接近原点的 K 个点

973. 最接近原点的 K 个点

此题有多种方法

  1. 计算出每一对点的欧几里得距离,之后排序:
class Solution { public int[][] kClosest(int[][] points, int k) { int n = points.length; int m = points[0].length; Arrays.sort(points, new Comparator<int[]>(){ public int compare(int[] point1, int[] point2) { return (point1[0] * point1[0] + point1[1] * point1[1]) - (point2[0] * point2[0] + point2[1] * point2[1]); } }); return Arrays.copyOfRange(points, 0, k); } }