Points on a Plane

Fill in the following methods:

public interface PointsOnAPlane {

    /**
     * Stores a given point in an internal data structure
     */
    void addPoint(Point point);

    /**
     * For given ‘center‘ point returns a subset of ‘m‘ stored points that are
     * closer to the center than others.
     *
     * E.g. Stored: (0, 1) (0, 2) (0, 3) (0, 4) (0, 5)
     *
     * findNearest(new Point(0, 0), 3) -> (0, 1), (0, 2), (0, 3)
     */
    Collection<Point> findNearest(Point center, int m);

}

解法一:用heap和hashmap,space用的好多啊……
HashMap<Points, Double> map;
private Comparator<Points> PointsComparator = new Comparator<Points>(){
    public int compare(Points A, Points B)
    {
        double distA = map.get(A);
        double distB = map.get(B);
        if(distA>distB) return 1;
        else if(distA<distB) return -1;
        else return 0;
    }
};

@Override
public ArrayList<Points> findNearest(Points center, int m) {
// TODO Auto-generated method stub
    map = new HashMap<Points,Double>();
    PriorityQueue<Points> q = new PriorityQueue<Points>(m,PointsComparator);
    for (Points p : points){
        double dist =  Math.pow((center.getX() - p.getX()),2) + Math.pow((center.getY() - p.getY()),2) ;
map.put(p,dist);
        q.add(p);
    }
    ArrayList<Points> nearestPoints = new ArrayList<Points>();
    for (int i = 0; i < m; i++){
        nearestPoints.add(q.poll());
    }
    return nearestPoints;
    }
            

完整版本见eclipse……

				
时间: 2024-08-02 15:11:37

Points on a Plane的相关文章

Programming Assignment 3: Collinear Points

The problem. Given a set of N distinct points in the plane, draw every (maximal) line segment that connects a subset of 4 or more of the points. Point data type. Create an immutable data type Point that represents a point in the plane by implementing

POJ1329 Circle Through Three Points(三角形外接圆)

题目链接: http://poj.org/problem?id=1329 题目描述: Circle Through Three Points Description Your team is to write a program that, given the Cartesian coordinates of three points on a plane, will find the equation of the circle through them all. The three poin

POJ 1329 Circle Through Three Points

Circle Through Three Points Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 3678   Accepted: 1542 Description Your team is to write a program that, given the Cartesian coordinates of three points on a plane, will find the equation of the

算法导论第四版学习——习题三Collinear Points

题目正文: http://coursera.cs.princeton.edu/algs4/assignments/collinear.html 作业难点: 1.仔细思考会感觉有很多实现方法,但是如果没有适当使用排序,算法时间复杂度就会轻易超过要求.(包括暴力算法) 2.隐含需要实现自己的数据结构用来组织“线”的集合,当然也可以用Stack或者Queue或者LinkedList,但是我个人是自己实现的. 3.由于一条线段只能出现一次,所以有一个去重的问题.(最难) 作业技巧: 1.基本按照题目的先

[Swift Weekly Contest 118]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 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 (exce

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 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 (exce

[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 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 guaran

LeetCode 973. K Closest Points to Origin

原题链接在这里: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

973. K Closest Points to Origin - Medium

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 (exce