Skip to content

Latest commit

 

History

History
169 lines (136 loc) · 5.4 KB

File metadata and controls

169 lines (136 loc) · 5.4 KB

English Version

题目描述

给你一个数组 points ,其中 points[i] = [xi, yi] ,表示第 i 个点在二维平面上的坐标。多个点可能会有 相同 的坐标。

同时给你一个数组 queries ,其中 queries[j] = [xj, yj, rj] ,表示一个圆心在 (xj, yj) 且半径为 rj 的圆。

对于每一个查询 queries[j] ,计算在第 j 个圆  点的数目。如果一个点在圆的 边界上 ,我们同样认为它在圆  。

请你返回一个数组 answer ,其中 answer[j]是第 j 个查询的答案。

 

示例 1:

输入:points = [[1,3],[3,3],[5,3],[2,2]], queries = [[2,3,1],[4,3,1],[1,1,2]]
输出:[3,2,2]
解释:所有的点和圆如上图所示。
queries[0] 是绿色的圆,queries[1] 是红色的圆,queries[2] 是蓝色的圆。

示例 2:

输入:points = [[1,1],[2,2],[3,3],[4,4],[5,5]], queries = [[1,2,2],[2,2,2],[4,3,2],[4,3,3]]
输出:[2,3,2,4]
解释:所有的点和圆如上图所示。
queries[0] 是绿色的圆,queries[1] 是红色的圆,queries[2] 是蓝色的圆,queries[3] 是紫色的圆。

 

提示:

  • 1 <= points.length <= 500
  • points[i].length == 2
  • 0 <= x​​​​​​i, y​​​​​​i <= 500
  • 1 <= queries.length <= 500
  • queries[j].length == 3
  • 0 <= xj, yj <= 500
  • 1 <= rj <= 500
  • 所有的坐标都是整数。

解法

计算每个点与每个圆的圆心之间的距离,若距离小于此圆的半径,说明该点在圆中。

Python3

class Solution:
    def countPoints(self, points: List[List[int]], queries: List[List[int]]) -> List[int]:
        ans = []
        for x0, y0, r in queries:
            count = 0
            for x, y in points:
                dx, dy = x - x0, y - y0
                if dx * dx + dy * dy <= r * r:
                    count += 1
            ans.append(count)
        return ans

Java

class Solution {
    public int[] countPoints(int[][] points, int[][] queries) {
        int[] ans = new int[queries.length];
        int i = 0;
        for (int[] query : queries) {
            int x0 = query[0], y0 = query[1], r = query[2];
            for (int[] point : points) {
                int x = point[0], y = point[1];
                int dx = x - x0, dy = y - y0;
                if (dx * dx + dy * dy <= r * r) {
                    ++ans[i];
                }
            }
            ++i;
        }
        return ans;
    }
}

TypeScript

function countPoints(points: number[][], queries: number[][]): number[] {
    let ans = [];
    for (let [cx, cy, r] of queries) {
        let square = r ** 2;
        let count = 0;
        for (let [px, py] of points) {
            if ((px - cx) ** 2 + (py - cy) ** 2 <= square) {
                ++count;
            }
        }
        ans.push(count);
    }
    return ans;
}

C++

class Solution {
public:
    vector<int> countPoints(vector<vector<int>>& points, vector<vector<int>>& queries) {
        vector<int> ans;
        for (auto& query : queries) {
            int x0 = query[0], y0 = query[1], r = query[2];
            int count = 0;
            for (auto& point : points) {
                int x = point[0], y = point[1];
                int dx = x - x0, dy = y - y0;
                if (dx * dx + dy * dy <= r * r) {
                    ++count;
                }
            }
            ans.push_back(count);
        }
        return ans;
    }
};

Go

func countPoints(points [][]int, queries [][]int) []int {
	ans := make([]int, len(queries))
	for i, query := range queries {
		x0, y0, r := query[0], query[1], query[2]
		for _, point := range points {
			x, y := point[0], point[1]
			dx, dy := x-x0, y-y0
			if dx*dx+dy*dy <= r*r {
				ans[i]++
			}
		}
	}
	return ans
}

...