973. K Closest Points to Origin
- K Closest Points to Origin python solution
题目描述
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.)
解析
经常做电云kNN搜索的我,看到这道题熟悉的笑了。
class Solution:
def kClosest(self, points: List[List[int]], K: int) -> List[List[int]]:
points.sort(key = lambda x: x[0]**2 + x[1] ** 2)
return points[:K]