题目地址:
https://leetcode.com/problems/queries-on-number-of-points-inside-a-circle/
给定 n n n个平面直角坐标系里的点,再给 q q q次询问,每次询问是两个整数 ( x , y , r ) (x,y,r) (x,y,r),问有多少个点在以 ( x , y ) (x,y) (x,y)为圆心, r r r为半径的圆里。在圆的边界上也算。
对每次询问直接暴力枚举。代码如下:
class Solution {
public:
vector<int> countPoints(vector<vector<int>>& points,
vector<vector<int>>& queries) {
vector<int> res(queries.size());
for (int i = 0; i < queries.size(); i++) {
auto& q = queries[i];
for (auto& p : points) {
int dx = p[0] - q[0], dy = p[1] - q[1], r = q[2];
if (dx * dx + dy * dy <= r * r) res[i]++;
}
}
return res;
}
};
时间复杂度 O ( n q ) O(nq) O(nq),空间 O ( 1 ) O(1) O(1)。