LintCode-[中等] 612. K个最近的点

来源:互联网 发布:淘宝宝贝长图作用 编辑:程序博客网 时间:2024/06/16 02:40

描述
给定一些 points 和一个 origin,从 points 中找到 k 个离 origin 最近的点。按照距离由小到大返回。如果两个点有相同距离,则按照x值来排序;若x值也相同,就再按照y值排序。

样例
给出 points = [[4,6],[4,7],[4,4],[2,5],[1,1]], origin = [0, 0], k = 3
返回 [[1,1],[2,5],[4,4]]

思路
运用pair同时记录points里的每个点的在points里的序号和到origin点的距离存入到一个vector中。结合pair定义一个符合题意的比较大小的函数,再运用快速排序将这个vector进行排序,获得排序结果,最后根据k的值决定输出结果的前k个点。

debug总结
快速排序运用不熟,map和pair的各自用法有点混淆。

C++实现:

/** * Definition for a point. * struct Point { *     int x; *     int y; *     Point() : x(0), y(0) {} *     Point(int a, int b) : x(a), y(b) {} * }; */class Solution{public:    /**    * @param points a list of points    * @param origin a point    * @param k an integer    * @return the k closest points    */    bool com(pair<int, float>& a, pair<int, float>& b, vector<Point>& points) {        if (a.second > b.second) {            return 1;        }        else if (a.second < b.second) {            return 0;        }        else {            if (points[a.first].x > points[b.first].x) {                return 1;            }            else if (points[a.first].x < points[b.first].x) {                return 0;            }            else {                if (points[a.first].y > points[b.first].y) {                    return 1;                }                else if (points[a.first].y < points[b.first].y) {                    return 0;                }                else {                    return 1;                }            }        }    }        int quickSort(vector<Point>& points, vector<pair<int, float>>& mapVector, int left, int right) {            if (left < right) {                int low = left;                int high = right;                pair<int, float>pivot = mapVector[left];                while (low < high) {                    while (low < high && com(mapVector[high], pivot, points)) {                        high--;                    }                    if (low < high) {                        mapVector[low] = mapVector[high];                    }                    while (low < high && com(pivot, mapVector[low], points)) {                        low++;                    }                    if (low < high) {                        mapVector[high] = mapVector[low];                    }                }                mapVector[low] = pivot;                return low;            }        }        void quickSort2(vector<Point>& points, vector<pair<int, float>>& mapVector, int left, int right) {            int size = mapVector.size();            if (size == 0) return;            if (left < right) {                int mid = quickSort(points, mapVector, left, right);                quickSort2(points, mapVector, left, mid - 1);                quickSort2(points, mapVector, mid + 1, right);            }        }        vector<Point> kClosest(vector<Point>& points, Point& origin, int k) {            // Write your code here            int size = points.size();            float distance;            vector<pair<int, float>>mapVector;            for (int i = 0; i < size; i++) {                distance = sqrt((points[i].x - origin.x) * (points[i].x - origin.x) + (points[i].y - origin.y) * (points[i].y - origin.y));                mapVector.push_back(make_pair(i, distance));            }            quickSort2(points, mapVector, 0, size - 1);            vector<Point>result;            for (int i = 0; i < k; i++) {                result.push_back(points[mapVector[i].first]);            }            return result;        }};
原创粉丝点击