K closest points
Find the K closest points to a target point in a 2D plane.
class Point {
public int x;
public int y; public Point(int x, int y) {
this.x = x;
this.y = y;
}
} class Solution { public List<Point> findKClosest(Point[] points, int k, Point p) { // max heap
PriorityQueue<Point> pq = new PriorityQueue<>(, new Comparator<Point>() {
@Override
public int compare(Point a, Point b) {
double d1 = distance(a, p);
double d2 = distance(b, p);
if (d1 == d2)
return ;
if (d1 < d2)
return ;
return -;
}
}); for (int i = ; i < points.length; i++) {
pq.offer(points[i]); if (pq.size() > k) {
pq.poll();
}
} List<Point> x = new ArrayList<>();
while (!pq.isEmpty())
x.add(pq.poll()); return x;
} private double distance(Point p1, Point p2) {
return Math.sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y));
}
}
K closest points的更多相关文章
- [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 ...
- [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 ...
- 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, ...
- Microsoft - Find the K closest points to the origin in a 2D plane
Find the K closest points to the origin in a 2D plane, given an array containing N points. 用 max hea ...
- LeetCode 973. K Closest Points to Origin
原题链接在这里:https://leetcode.com/problems/k-closest-points-to-origin/ 题目: We have a list of points on th ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- __definedGetter\Setter__的一些想法
__definedGetter\Setter__ 是JS5在创建对象后内置的方法,用于在读写对象属性的时候执行的方法. zhangmingzhi.__defineSetter__('age',func ...
- NOI2018准备Day13晚
今晚很困.很困,看题解做了一道钻石级的题,数独发生了神奇的错误,=_=
- ubuntu系统升级记录
之前在openstack中安装了ubuntu 12.04虚拟机,版本较低,需要升级为高版本.下面分享下升级过程: ubuntu系统升级操作:$ cat /etc/issueUbuntu 12.04.5 ...
- C# Memcache分布式缓存简单入门
什么是Memcache?能做什么? 以下是百度的观点: memcache是一套分布式的高速缓存系统,由LiveJournal的Brad Fitzpatrick开发,但目前被许多网站使用以提升网站的访问 ...
- [转]Java线程安全总结
最近想将java基础的一些东西都整理整理,写下来,这是对知识的总结,也是一种乐趣.已经拟好了提纲,大概分为这几个主题: java线程安全,java垃圾收集,java并发包详细介绍,java profi ...
- 精通Web Analytics 2.0 (5) 第三章:点击流分析的奇妙世界:指标
精通Web Analytics 2.0 : 用户中心科学与在线统计艺术 第三章:点击流分析的奇妙世界:指标 新的Web Analytics 2.0心态:搞定它.新的闪亮系列工具:是的.准备好了吗?当然 ...
- Python 面向对象 基础
编程范式概述:面向过程 和 面向对象 以及函数式编程 面向过程:(Procedure Oriented)是一种以事件为中心的编程思想. 就是分析出解决问题所需要的步骤,然后用函数把这些步骤一步一步实现 ...
- Javascript知识点记录(二)
Javascript入门易,精通难,基本上是共识的一个观点.在这个篇幅里,主要对一些难点进行记录. 鸭子类型 Javascript属于动态类型语言的一种.对变量类型的宽容,给了很大的灵活性.由于无需类 ...
- 使用IExport进行图片输出出现File creation error
使用IExport进行图片输出(.JPG)时,出现如下异常File creation error. 在ESRI.ArcGIS.Output.ExportJPEGClass.FinishExport ...
- TCP/IP——基本知识
TCP / IP通常被认为是一个四层协议系统: 1) 链路层,有时也称作数据链路层或网络接口层,通常包括操作系统中的设备驱动程序和计算机中对应的网络接口卡.它们一起处理与电缆(或其他任何传输媒介)的物 ...