1828:统计一个圆中点的数目

This commit is contained in:
huangge1199 2021-06-07 13:03:58 +08:00
parent 7dd35f6164
commit e3c43974ce
2 changed files with 118 additions and 0 deletions

View File

@ -0,0 +1,77 @@
//给你一个数组 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 <= xi, yi <= 500
// 1 <= queries.length <= 500
// queries[j].length == 3
// 0 <= xj, yj <= 500
// 1 <= rj <= 500
// 所有的坐标都是整数
//
// Related Topics 数学
// 👍 5 👎 0
package leetcode.editor.cn;
//1828:统计一个圆中点的数目
public class QueriesOnNumberOfPointsInsideACircle{
public static void main(String[] args) {
//测试代码
Solution solution = new QueriesOnNumberOfPointsInsideACircle().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int[] countPoints(int[][] points, int[][] queries) {
int size = queries.length;
int[] result = new int[size];
for (int i = 0; i < size; i++) {
int num = 0;
int length = points.length;
int c = queries[i][2];
for (int[] point : points) {
int x = Math.abs(queries[i][0] - point[0]);
int y = Math.abs(queries[i][1] - point[1]);
if (x * x + y * y <= c * c) {
num++;
}
}
result[i] = num;
}
return result;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,41 @@
<p>给你一个数组 <code>points</code> ,其中 <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> ,表示第 <code>i</code> 个点在二维平面上的坐标。多个点可能会有 <strong>相同</strong> 的坐标。</p>
<p>同时给你一个数组 <code>queries</code> ,其中 <code>queries[j] = [x<sub>j</sub>, y<sub>j</sub>, r<sub>j</sub>]</code> ,表示一个圆心在 <code>(x<sub>j</sub>, y<sub>j</sub>)</code> 且半径为 <code>r<sub>j</sub></code><sub> </sub>的圆。</p>
<p>对于每一个查询 <code>queries[j]</code> ,计算在第 <code>j</code> 个圆 <strong></strong> 点的数目。如果一个点在圆的 <strong>边界上</strong> ,我们同样认为它在圆 <strong></strong> 。</p>
<p>请你返回一个数组<em> </em><code>answer</code> ,其中<em> </em><code>answer[j]</code>是第 <code>j</code> 个查询的答案。</p>
<p> </p>
<p><strong>示例 1</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/25/chrome_2021-03-25_22-34-16.png" style="width: 500px; height: 418px;">
<pre><b>输入:</b>points = [[1,3],[3,3],[5,3],[2,2]], queries = [[2,3,1],[4,3,1],[1,1,2]]
<b>输出:</b>[3,2,2]
<b>解释:</b>所有的点和圆如上图所示。
queries[0] 是绿色的圆queries[1] 是红色的圆queries[2] 是蓝色的圆。
</pre>
<p><strong>示例 2</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/25/chrome_2021-03-25_22-42-07.png" style="width: 500px; height: 390px;">
<pre><b>输入:</b>points = [[1,1],[2,2],[3,3],[4,4],[5,5]], queries = [[1,2,2],[2,2,2],[4,3,2],[4,3,3]]
<b>输出:</b>[2,3,2,4]
<b>解释:</b>所有的点和圆如上图所示。
queries[0] 是绿色的圆queries[1] 是红色的圆queries[2] 是蓝色的圆queries[3] 是紫色的圆。
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= points.length &lt;= 500</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>0 &lt;= x<sub>i</sub>, y<sub>i</sub> &lt;= 500</code></li>
<li><code>1 &lt;= queries.length &lt;= 500</code></li>
<li><code>queries[j].length == 3</code></li>
<li><code>0 &lt;= x<sub>j</sub>, y<sub>j</sub> &lt;= 500</code></li>
<li><code>1 &lt;= r<sub>j</sub> &lt;= 500</code></li>
<li>所有的坐标都是整数。</li>
</ul>
<div><div>Related Topics</div><div><li>数学</li></div></div>\n<div><li>👍 5</li><li>👎 0</li></div>