• 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 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 (except for the order that it is in.)

    Example 1:

    Input: points = [[1,3],[-2,2]], K = 1
    Output: [[-2,2]]
    Explanation: 
    The distance between (1, 3) and the origin is sqrt(10).
    The distance between (-2, 2) and the origin is sqrt(8).
    Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
    We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]].
    

    Example 2:

    Input: points = [[3,3],[5,-1],[-2,4]], K = 2
    Output: [[3,3],[-2,4]]
    (The answer [[-2,4],[3,3]] would also be accepted.)
    

    Note:

    1. 1 <= K <= points.length <= 10000
    2. -10000 < points[i][0] < 10000
    3. -10000 < points[i][1] < 10000

    没啥好说的,写的差劲了

    struct P{
        double dis;
        int adr;
    }H[20000];
    bool cmd(P x,P y){
        if(x.dis <= y.dis){
            return 1;
        }
        return 0;
    }
    class Solution {
    public:
        vector<vector<int>> kClosest(vector<vector<int>>& points, int K) {
            vector<vector<int>> x;
            vector<int>y;
            
            int Len = points.size();
            for(int i=0;i<Len;i++){
                int a = points[i][0];
                int b = points[i][1];
                //cout<<a<<" "<<b<<endl;
                double result = sqrt(a*a+b*b);
                H[i].dis = result;
                H[i].adr = i;
            }
            sort(H,H+Len,cmd);
            for(int i=0;i<K;i++){
                x.push_back(points[H[i].adr]);
            }
            return x;
        }
    };
  • 相关阅读:
    权限认证机制
    在线工具统计
    Redis内存模型
    Redis数据结构
    开发工具清单
    MySql数据库优化、备份和恢复
    MySql 性能优化神器 Explain
    ASP.NET Core 3.1 迁移到 NET 5.0
    MySql Sql语句
    DTU的通讯工作模式有哪些
  • 原文地址:https://www.cnblogs.com/yinghualuowu/p/10263542.html
Copyright © 2020-2023  润新知