rocksc30 / LeetCode

用于力扣刷题打卡
2 stars 0 forks source link

1828. 统计一个圆中点的数目 #43

Open Ni-Guvara opened 1 year ago

Ni-Guvara commented 1 year ago
class Solution {
public:
    vector<int> countPoints(vector<vector<int>>& points, vector<vector<int>>& queries) {

        int cnt = queries.size();
        int len = points.size();

        vector<int>result(cnt);

        for(int i = 0 ; i < cnt ;i++)
        {
            for(int j = 0; j < len ; j++)
            {
                if(isInCir(queries[i],points[j][0],points[j][1]))
                    ++result[i];
            }
        }

        return result;

    }

    bool isInCir(vector<int> query,int x,int y)
    {
        int x1 = query[0];
        int y1 = query[1];
        int r = query[2];

        if((x1 - x)*(x1 - x) + (y1 - y) * (y1 - y) > r * r)
            return false;
        return true;
    }
};
Ni-Guvara commented 1 year ago

简单难度的中等题

rocksc30 commented 1 year ago

非常简单难度的中等难度题

class Solution {
    public int[] countPoints(int[][] points, int[][] queries) {
        int[] res = new int[queries.length];
        for(int i = 0; i < queries.length; i++){
            for(int j = 0; j < points.length; j++){
                if(inCircle(points[j], queries[i])){
                    res[i]++;
                }
            }
        }
        return res;
    }
    boolean inCircle(int[] point, int[] circle){
        // 圆心坐标
        int cx = circle[0], cy = circle[1];
        // 点坐标
        int px = point[0], py = point[1];
        //两点距离的平方
        int distance = (cy - py) * (cy - py) + (cx - px) * (cx - px);

        // 半径
        int r = circle[2];
        return distance <= r * r ? true : false;
    }
}
rocksc30 commented 1 year ago

你写代码能不能学习我的, 可读性!!!

Ni-Guvara commented 1 year ago

我的真的那么不好看懂?