LeetCode 973 K Closest Points to Origin 解题报告

时间:2022-02-18 03:22:55

题目要求

We have a list of points on the plane.  Find the K closest points to the origin (0, 0).

(Here, the distance between two points on a plane is the Euclidean distance.)

You may return the answer in any order.  The answer is guaranteed to be unique (except for the order that it is in.)

题目分析及思路

题目要求返回离原点最近的K个点的坐标。可使用sort函数进行排序。

python代码​

class Solution:

def kClosest(self, points, K):

"""

:type points: List[List[int]]

:type K: int

:rtype: List[List[int]]

"""

points.sort(key = lambda P:P[0]**2+P[1]**2)

return points[:K]