K Closest Points Lintcode

Given some points and a point origin in two dimensional space, find k points out of the some points which are nearest to origin.
Return these points sorted by distance, if they are same with distance, sorted by x-axis, otherwise sorted by y-axis.

Example

Given points = [[4,6],[4,7],[4,4],[2,5],[1,1]], origin = [0, 0], k = 3
return [[1,1],[2,5],[4,4]]

 

这道题思路很简单,debug de了好久真是好气哦!算距离的时候一开始用了Math.sqrt()这个函数,转成double,减完之后转成int,但是这样的话9.05 - 8.78转完了就是0,heap里面就会乱排。。。我以为heap排序不工作了呢。。。想了半天是怎么回事。。。这样看来还是不要遇到问题就怀疑人生。。。一般要从最基本的问题找起。

精度真是要注意再注意啊。。。

另外其实不用开根号的,这样就不会有损失精度的问题了。。。因为开根号可能不一样的减成0,然后又乱排了。。。

复制代码
/**
 * Definition for a point.
 * class Point {
 *     int x;
 *     int y;
 *     Point() { x = 0; y = 0; }
 *     Point(int a, int b) { x = a; y = b; }
 * }
 */
public class Solution {
    /**
     * @param points a list of points
     * @param origin a point
     * @param k an integer
     * @return the k closest points
     */
    Point origin;
    public Point[] kClosest(Point[] points, Point original, int k) {
        origin = original;
        PriorityQueue<Point> pq = new PriorityQueue<>(k, new Comparator<Point>() {
            public int compare(Point a, Point b) {
                int disa = (int) (Math.pow((a.x - origin.x),2) + Math.pow((a.y - origin.y), 2));
                int disb = (int) (Math.pow((b.x - origin.x),2) + Math.pow((b.y - origin.y), 2));
                int sub = disb - disa;
                if (sub == 0) {
                    sub = b.x - a.x;
                    if (sub == 0) {
                        sub = b.y - a.y;
                    }
                }
                return sub;
            }
        });
        for (int i = 0; i < points.length; i++) {
            if (pq.size() < k) {
                pq.offer(points[i]);
            } else {
                double disThis = (int) (Math.pow((points[i].x - origin.x),2) + Math.pow((points[i].y - origin.y), 2));
                Point peeked = pq.peek();
                double qPeek = (int) (Math.pow((peeked.x - origin.x),2) + Math.pow((peeked.y - origin.y), 2));
                if (disThis < qPeek) {
                    pq.poll();
                    pq.offer(points[i]);
                }
            }
        }
        Point[] result = new Point[k];
        int i = k - 1;
        while (!pq.isEmpty()) {
            result[i] = pq.poll();
            i--;
        }
        return result;
    }
}
复制代码

另外其实不用这么麻烦,直接控制到k的个数就可以了。

复制代码
/**
 * Definition for a point.
 * class Point {
 *     int x;
 *     int y;
 *     Point() { x = 0; y = 0; }
 *     Point(int a, int b) { x = a; y = b; }
 * }
 */
public class Solution {
    /**
     * @param points a list of points
     * @param origin a point
     * @param k an integer
     * @return the k closest points
     */
    Point origin;
    public Point[] kClosest(Point[] points, Point original, int k) {
        origin = original;
        PriorityQueue<Point> pq = new PriorityQueue<>(k, new Comparator<Point>() {
            public int compare(Point a, Point b) {
                int disa = (int) (Math.pow((a.x - origin.x),2) + Math.pow((a.y - origin.y), 2));
                int disb = (int) (Math.pow((b.x - origin.x),2) + Math.pow((b.y - origin.y), 2));
                int sub = disb - disa;
                if (sub == 0) {
                    sub = b.x - a.x;
                    if (sub == 0) {
                        sub = b.y - a.y;
                    }
                }
                return sub;
            }
        });
        for (int i = 0; i < points.length; i++) {
            pq.offer(points[i]);
            if (pq.size() > k) {
                pq.poll();
            }
        }
        Point[] result = new Point[k];
        int i = k - 1;
        while (!pq.isEmpty()) {
            result[i] = pq.poll();
            i--;
        }
        return result;
    }
}
复制代码

 

posted @   璨璨要好好学习  阅读(1632)  评论(0编辑  收藏  举报
编辑推荐:
· .NET 依赖注入中的 Captive Dependency
· .NET Core 对象分配(Alloc)底层原理浅谈
· 聊一聊 C#异步 任务延续的三种底层玩法
· 敏捷开发:如何高效开每日站会
· 为什么 .NET8线程池 容易引发线程饥饿
阅读排行:
· 一个适用于 .NET 的开源整洁架构项目模板
· API 风格选对了,文档写好了,项目就成功了一半!
· 【开源】C#上位机必备高效数据转换助手
· .NET 9.0 使用 Vulkan API 编写跨平台图形应用
· MyBatis中的 10 个宝藏技巧!
点击右上角即可分享
微信分享提示