find k closest point to origin point in 2d plane
2016-07-18 08:48
411 查看
public List<Point> findKClosest(Point[] p, int k) { List<Point> res = new LinkedList<>(); PriorityQueue<Point> queue = new PriorityQueue<>(11, new Comparator<Point>(){ @Override public int compare(Point a, Point b){ return b.x * b.x + b.y * b.y - a.x * a.x - a.y * a.y; } }); for (Point point: p) { if (queue.size() < k) { queue.offer(point); } else { Point a = queue.poll(); if (point.x * point.x + point.y * point.y - a.x * a.x - a.y * a.y < 0) { queue.poll(); queue.offer(point); } } } while (!queue.isEmpty()) { res.add(queue.poll()); } return res; } class Point { int x; int y; public Point (int x, int y) { this.x = x; this.y = y; } }
相关文章推荐
- 上周热点回顾(7.11-7.17)
- ABAP实战Smart Forms设计
- M8100-Table T169P: entry 1510 does not exist
- 三相异步电机检测技术的应用研究
- Hadoop2.x实战:WordCount、Sort、去重复、average实例MapRedure编写
- 随笔
- xcode菜单功能翻译
- Java并发编程与技术内幕:聊聊锁的技术内幕(中)
- iOS动画(Core Animation)总结
- Iterator
- Spark日志分析案例
- Leetcode 319 : Bulb Switcher
- CodeForces 609C Load Balancing
- CocosCreator
- JavaScript获取css行间样式,内连样式和外链样式的简单方法
- SDUT 2133 数据结构实验之栈三:后缀式求值
- 杭电ACM--HDU-1021斐波拉契
- python学习——TCP编程
- JZOJ1444. 交换【推荐】
- 接口总结