• 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;
        }
    };
  • 相关阅读:
    写在寒假
    Docker安装Redis(两种方式)
    JVM类加载机制
    RabbitMQ学习
    MySQL索引详解
    Java内存区域
    Window10下的Docker Desktop安装(保姆级教程)
    JVM垃圾回收
    力扣Hot100(目前进度89/100)
    C# 发送邮件实例
  • 原文地址:https://www.cnblogs.com/yinghualuowu/p/10263542.html
Copyright © 2020-2023  润新知