- K Closest Points to Origin题目链接973. K Closest Points to Origin题目分析给一个坐标数组points,从中返回K个离0,0最近的坐标。其中,用欧几里得距离计算。思路把距离作为数组的键,把对应坐标作为数组的值。用ksort函数排序,再用array_slice函数获取前K个即可。最终代码<?phpclass Solution { function kClosest($points, $K) { $dists = []; foreach($points as $point){ $dists[(string)sqrt(pow($point[0],2)+pow($point[1],2))] = $point; } ksort($dists); return array_slice($dists,0,$K); }}若觉得本文章对你有用,欢迎用爱发电资助。