题目描述:

可参考:题215

方法一:排序

class Solution:
def kClosest(self, points: List[List[int]], K: int) -> List[List[int]]:
points.sort(key = lambda P: P[0]**2 + P[1]**2)
return points[:K]

快排+分治:

class Solution:
def kClosest(self, points: List[List[int]], K: int) -> List[List[int]]:
# 计算欧几里得距离
distance = lambda i: points[i][0] ** 2 + points[i][1] ** 2 def work(i, j, K):
if i > j:
return
# 记录初始值
oi, oj = i, j
# 取最左边为哨兵值
pivot = distance(oi)
while i != j:
while i < j and distance(j) >= pivot:
j -= 1
while i < j and distance(i) <= pivot:
i += 1
if i < j:
# 交换值
points[i], points[j] = points[j], points[i] # 交换哨兵
points[i], points[oi] = points[oi], points[i] # 递归
if K <= i - oi + 1:
# 左半边排序
work(oi, i - 1, K)
else:
# 右半边排序
work(i + 1, oj, K - (i - oi + 1)) work(0, len(points) - 1, K)
return points[:K]

方法三:堆排序

from heapq import heappush, heappop 

class Solution:
def kClosest(self, points: List[List[int]], K: int) -> List[List[int]]:
queue = []
distance = lambda x: points[x][0]**2 + points[x][1]**2
length = len(points)
for i in range(length):
heappush(queue, (distance(i), points[i]))
res = []
for i in range(K):
res.append(heappop(queue)[1])
return res

leetcode-973-最接近原点的K个点的更多相关文章

  1. LeetCode——973. 最接近原点的 K 个点

    我们有一个由平面上的点组成的列表 points.需要从中找出 K 个距离原点 (0, 0) 最近的点. (这里,平面上两点之间的距离是欧几里德距离.) 你可以按任何顺序返回答案.除了点坐标的顺序之外, ...

  2. 【力扣】973. 最接近原点的 K 个点

    我们有一个由平面上的点组成的列表 points.需要从中找出 K 个距离原点 (0, 0) 最近的点. (这里,平面上两点之间的距离是欧几里德距离.) 你可以按任何顺序返回答案.除了点坐标的顺序之外, ...

  3. 973. 最接近原点的 K 个点

    1.暴力排序,新建节点类重载小于符号排序. class Solution { public: struct comb{ int index,distance; comb():index(0),dist ...

  4. leetcode-973最接近原点的K个点

    leetcode-973最接近原点的K个点 题意 我们有一个由平面上的点组成的列表 points.需要从中找出 K 个距离原点 (0, 0) 最近的点. (这里,平面上两点之间的距离是欧几里德距离.) ...

  5. [Swift]LeetCode973. 最接近原点的 K 个点 | K Closest Points to Origin

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

  6. 最接近原点的K个点

    一.题目描述 我们有一个由平面上的点组成的列表 points.需要从中找出 K 个距离原点 (0, 0) 最近的点 这里,平面上两点之间的距离是欧几里德距离 你可以按任何顺序返回答案.除了点坐标的顺序 ...

  7. Leetcode973. K Closest Points to Origin最接近原点的K个点

    我们有一个由平面上的点组成的列表 points.需要从中找出 K 个距离原点 (0, 0) 最近的点. (这里,平面上两点之间的距离是欧几里德距离.) 你可以按任何顺序返回答案.除了点坐标的顺序之外, ...

  8. LeetCode:数组中的第K个最大元素【215】

    LeetCode:数组中的第K个最大元素[215] 题目描述 在未排序的数组中找到第 k 个最大的元素.请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素. 示例 1: ...

  9. LeetCode:最接近的三数之和【16】

    LeetCode:最接近的三数之和[16] 题目描述 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这 ...

随机推荐

  1. JDK8新特性之函数式接口

    什么是函数式接口 先来看看传统的创建线程是怎么写的 Thread t1 = new Thread(new Runnable() { @Override public void run() { Syst ...

  2. [未解决]报错:SSLError

    参考网友解决的方法 任何报SSLError类的错,解决方法: 引入ssl模块 import ssl 在url链接代码上方添加语句: ssl._create_default_https_context ...

  3. Reqests----Get请求之参数化

    一.环境安装 >>pip install requests 注意:pip很容易就会版本升级,如果用python3的,请使用pip3 install requests 1.初始化版本 2.把 ...

  4. HTTP信息头处理器

    就是HTTP请求头-Header

  5. Python学习笔记(七)——魔法方法

    1.构造和析造 魔法方法就是被双下划线包围的方法 __init__()方法 __init__方法默认没有参数,返回值为none.类实例化对象需有明确的初始化步骤要重写函数 >>> c ...

  6. on windows in superset sql lab error "module object has no attribute sigalrm"

    改下  utils.py   文件 It works after doing the following change (sorry for the massed up alignment, prob ...

  7. Struts功能详解——ValidatorForm

    ActionForm和ValidatorForm区别:       一个Form继承了ValidatorForm 就不用写具体的验证,但是需要提供:validation-rules.xml 和 val ...

  8. iOS 多层级的immutable objects 转换成 mutable objects

    第一种方法是:将多层级的递归转换 方法: +(id) recursiveMutable:(id)object { if([object isKindOfClass:[NSDictionary clas ...

  9. bat命令自用其(一)

    每秒钟打印ping命令结果到指定文件: @echo off set /p ip=Input the IP required to monitor: :starts echo %date% %time% ...

  10. pycharm 使用心得(一)安装和首次使用

    PyCharm 是我用过的python编辑器中,比较顺手的一个.而且可以跨平台,在macos和windows下面都可以用,这点比较好. 首先预览一下 PyCharm 在实际应用中的界面:(更改了PyC ...