【LeetCode】973. K Closest Points to Origin 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/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 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.)
Example 1:
Input: points = [[1,3],[-2,2]], K = 1
Output: [[-2,2]]
Explanation:
The distance between (1, 3) and the origin is sqrt(10).
The distance between (-2, 2) and the origin is sqrt(8).
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]].
Example 2:
Input: points = [[3,3],[5,-1],[-2,4]], K = 2
Output: [[3,3],[-2,4]]
(The answer [[-2,4],[3,3]] would also be accepted.)
Note:
1 <= K <= points.length <= 10000
-10000 < points[i][0] < 10000
-10000 < points[i][1] < 10000
题目大意
找出离原点(0, 0)
最近的K个点。
解题方法
小根堆
经典的TopK,这个题的做法很多,最常见的就是使用小根堆。因为这是周赛,为了节省时间,我就直接使用了python的小根堆。
首先把每个元素距离原点的距离和该坐标组成tuple放到list里面,这样构建堆的时候,会按照第一个元素自动排序。提供了nsmallest方法直接取出最小的K个tuple,然后把坐标返回即可。
关于TopK,可以看拜托,面试别再问我TopK了!!!
python代码如下:
class Solution(object):
def kClosest(self, points, K):
"""
:type points: List[List[int]]
:type K: int
:rtype: List[List[int]]
"""
dis = []
for p in points:
d = math.sqrt(p[0] ** 2 + p[1] ** 2)
dis.append((d, p))
heapq.heapify(dis)
return [d[1] for d in heapq.nsmallest(K, dis)]
日期
2019 年 1 月 13 日 —— 时间太快了
【LeetCode】973. K Closest Points to Origin 解题报告(Python)的更多相关文章
- LeetCode 973 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, ...
- LeetCode 973. K Closest Points to Origin
原题链接在这里:https://leetcode.com/problems/k-closest-points-to-origin/ 题目: We have a list of points on th ...
- 【leetcode】973. K Closest Points to Origin
题目如下: We have a list of points on the plane. Find the Kclosest points to the origin (0, 0). (Here, ...
- [Solution] 973. K Closest Points to Origin
Difficulty: Easy Problem We have a list of points on the plane. Find the K closest points to the ori ...
- 973. 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 ...
- LC 973. 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 ...
- 119th LeetCode Weekly Contest 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 ...
- [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 ...
- 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 ...
随机推荐
- 什么是总线、总线的类型、局部总线、局部总线类型和什么是接口方式?什么是IDE?什么是SCSI?
在安装电脑系统时,进行内核配置时涉及到各种总线类型,有必要了解一下什么是总线.总线的类型.局部总线.局部总线类型和接口方式. 1)总线总线是一组通信线.在机器内部, 各部件通过总线连接; 对于外部设备 ...
- 57-Palindrome Linked List
Palindrome Linked List My Submissions QuestionEditorial Solution Total Accepted: 46990 Total Submiss ...
- 监督学习&非监督学习
监督学习 1 - 3 - Supervised Learning 在监督学习中,数据集中的每个例子,算法将预测得到例子的""正确答案"",像房子的价格,或者溜 ...
- leetcode刷题之数组NO.4
1.题目 给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序. 示例: 输入: [0,1,0,3,12] 输出: [1,3,12,0,0] 说明: 必须在原数 ...
- 乱序拼图验证的识别并还原-puzzle-captcha
一.前言 乱序拼图验证是一种较少见的验证码防御,市面上更多的是拖动滑块,被完美攻克的有不少,都在行为轨迹上下足了功夫,本文不讨论轨迹模拟范畴,就只针对拼图还原进行研究. 找一个市面比较普及的顶像乱序拼 ...
- Maven pom.xml报错解决
用Maven建了一个web工程,总是在pom.xml头的地方报错: 大概是: Original error: Could not transfer artifact org.hamcrest:hamc ...
- Swift3.0 延时执行
//延时1s执行 DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + Double(Int64(1*NSEC_PER_SEC))/ ...
- java多线程 并发编程
一.多线程 1.操作系统有两个容易混淆的概念,进程和线程. 进程:一个计算机程序的运行实例,包含了需要执行的指令:有自己的独立地址空间,包含程序内容和数据:不同进程的地址空间是互相隔离的:进程拥有各种 ...
- android 调用相机拍照及相册
调用系统相机拍照: private Button btnDyxj; private ImageView img1; private File tempFile; btnDyxj = (Button) ...
- java web 限制同一个用户在不同处登入
用到的技术:map集合,sessionListener监听器,Fiter过滤器. 实现思路: 一.利用一个全局的map集合来保存每个用户sessionID的值的一个集合.一个用户对应一个session ...