for i, (cx, cy, cr) inenumerate(queries): for (px, py) in points: if (cx - px) ** 2 + (cy - py) ** 2 <= cr ** 2: ans[i] += 1 return ans
[sol1-C]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
int* countPoints(int** points, int pointsSize, int* pointsColSize, int** queries, int queriesSize, int* queriesColSize, int* returnSize) { int *ans = (int *)malloc(sizeof(int) * queriesSize); memset(ans, 0, sizeof(int) * queriesSize); for (int i = 0; i < queriesSize; ++i) { int cx = queries[i][0], cy = queries[i][1], cr = queries[i][2]; for (int j = 0; j < pointsSize; ++j) { int px = points[j][0], py = points[j][1]; if ((cx - px) * (cx - px) + (cy - py) * (cy - py) <= cr * cr) { ++ans[i]; } } } *returnSize = queriesSize; return ans; }
[sol1-JavaScript]
1 2 3 4 5 6 7 8 9 10 11 12 13 14
var countPoints = function(points, queries) { const m = points.length, n = queries.length; const ans = newArray(n).fill(0); for (let i = 0; i < n; ++i) { let cx = queries[i][0], cy = queries[i][1], cr = queries[i][2]; for (let j = 0; j < m; ++j) { let px = points[j][0], py = points[j][1]; if ((cx - px) * (cx - px) + (cy - py) * (cy - py) <= cr * cr) { ++ans[i]; } } } return ans; };
[sol1-Golang]
1 2 3 4 5 6 7 8 9 10 11 12
funccountPoints(points [][]int, queries [][]int) []int { ans := make([]int, len(queries)) for i, q := range queries { x, y, r := q[0], q[1], q[2] for _, p := range points { if (p[0]-x)*(p[0]-x)+(p[1]-y)*(p[1]-y) <= r*r { ans[i]++ } } } return ans }