Skip to content

Commit 01ac8a6

Browse files
committed
feat: add solutions to lc problem: No.1828. Queries on Number of Points Inside a Circle
1 parent e9d296b commit 01ac8a6

File tree

6 files changed

+206
-4
lines changed

6 files changed

+206
-4
lines changed

solution/1800-1899/1828.Queries on Number of Points Inside a Circle/README.md

+73-2
Original file line numberDiff line numberDiff line change
@@ -47,27 +47,98 @@ queries[0] 是绿色的圆,queries[1] 是红色的圆,queries[2] 是蓝色
4747
<li>所有的坐标都是整数。</li>
4848
</ul>
4949

50-
5150
## 解法
5251

5352
<!-- 这里可写通用的实现逻辑 -->
5453

54+
计算每个点与每个圆的圆心之间的距离,若距离小于此圆的半径,说明该点在圆中。
55+
5556
<!-- tabs:start -->
5657

5758
### **Python3**
5859

5960
<!-- 这里可写当前语言的特殊实现逻辑 -->
6061

6162
```python
62-
63+
class Solution:
64+
def countPoints(self, points: List[List[int]], queries: List[List[int]]) -> List[int]:
65+
ans = []
66+
for x0, y0, r in queries:
67+
count = 0
68+
for x, y in points:
69+
dx, dy = x - x0, y - y0
70+
if dx * dx + dy * dy <= r * r:
71+
count += 1
72+
ans.append(count)
73+
return ans
6374
```
6475

6576
### **Java**
6677

6778
<!-- 这里可写当前语言的特殊实现逻辑 -->
6879

6980
```java
81+
class Solution {
82+
public int[] countPoints(int[][] points, int[][] queries) {
83+
int[] ans = new int[queries.length];
84+
int i = 0;
85+
for (int[] query : queries) {
86+
int x0 = query[0], y0 = query[1], r = query[2];
87+
for (int[] point : points) {
88+
int x = point[0], y = point[1];
89+
int dx = x - x0, dy = y - y0;
90+
if (dx * dx + dy * dy <= r * r) {
91+
++ans[i];
92+
}
93+
}
94+
++i;
95+
}
96+
return ans;
97+
}
98+
}
99+
```
100+
101+
### **C++**
102+
103+
```cpp
104+
class Solution {
105+
public:
106+
vector<int> countPoints(vector<vector<int>>& points, vector<vector<int>>& queries) {
107+
vector<int> ans;
108+
for (auto& query : queries) {
109+
int x0 = query[0], y0 = query[1], r = query[2];
110+
int count = 0;
111+
for (auto& point : points) {
112+
int x = point[0], y = point[1];
113+
int dx = x - x0, dy = y - y0;
114+
if (dx * dx + dy * dy <= r * r) {
115+
++count;
116+
}
117+
}
118+
ans.push_back(count);
119+
}
120+
return ans;
121+
}
122+
};
123+
```
70124
125+
### **Go**
126+
127+
```go
128+
func countPoints(points [][]int, queries [][]int) []int {
129+
ans := make([]int, len(queries))
130+
for i, query := range queries {
131+
x0, y0, r := query[0], query[1], query[2]
132+
for _, point := range points {
133+
x, y := point[0], point[1]
134+
dx, dy := x-x0, y-y0
135+
if dx*dx+dy*dy <= r*r {
136+
ans[i]++
137+
}
138+
}
139+
}
140+
return ans
141+
}
71142
```
72143

73144
### **...**

solution/1800-1899/1828.Queries on Number of Points Inside a Circle/README_EN.md

+71-2
Original file line numberDiff line numberDiff line change
@@ -48,21 +48,90 @@ queries[0] is green, queries[1] is red, queries[2] is blue, and queries[3] is pu
4848
<p>&nbsp;</p>
4949
<p><strong>Follow up:</strong> Could you find the answer for each query in better complexity than <code>O(n)</code>?</p>
5050

51-
5251
## Solutions
5352

5453
<!-- tabs:start -->
5554

5655
### **Python3**
5756

5857
```python
59-
58+
class Solution:
59+
def countPoints(self, points: List[List[int]], queries: List[List[int]]) -> List[int]:
60+
ans = []
61+
for x0, y0, r in queries:
62+
count = 0
63+
for x, y in points:
64+
dx, dy = x - x0, y - y0
65+
if dx * dx + dy * dy <= r * r:
66+
count += 1
67+
ans.append(count)
68+
return ans
6069
```
6170

6271
### **Java**
6372

6473
```java
74+
class Solution {
75+
public int[] countPoints(int[][] points, int[][] queries) {
76+
int[] ans = new int[queries.length];
77+
int i = 0;
78+
for (int[] query : queries) {
79+
int x0 = query[0], y0 = query[1], r = query[2];
80+
for (int[] point : points) {
81+
int x = point[0], y = point[1];
82+
int dx = x - x0, dy = y - y0;
83+
if (dx * dx + dy * dy <= r * r) {
84+
++ans[i];
85+
}
86+
}
87+
++i;
88+
}
89+
return ans;
90+
}
91+
}
92+
```
93+
94+
### **C++**
95+
96+
```cpp
97+
class Solution {
98+
public:
99+
vector<int> countPoints(vector<vector<int>>& points, vector<vector<int>>& queries) {
100+
vector<int> ans;
101+
for (auto& query : queries) {
102+
int x0 = query[0], y0 = query[1], r = query[2];
103+
int count = 0;
104+
for (auto& point : points) {
105+
int x = point[0], y = point[1];
106+
int dx = x - x0, dy = y - y0;
107+
if (dx * dx + dy * dy <= r * r) {
108+
++count;
109+
}
110+
}
111+
ans.push_back(count);
112+
}
113+
return ans;
114+
}
115+
};
116+
```
65117
118+
### **Go**
119+
120+
```go
121+
func countPoints(points [][]int, queries [][]int) []int {
122+
ans := make([]int, len(queries))
123+
for i, query := range queries {
124+
x0, y0, r := query[0], query[1], query[2]
125+
for _, point := range points {
126+
x, y := point[0], point[1]
127+
dx, dy := x-x0, y-y0
128+
if dx*dx+dy*dy <= r*r {
129+
ans[i]++
130+
}
131+
}
132+
}
133+
return ans
134+
}
66135
```
67136

68137
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
vector<int> countPoints(vector<vector<int>>& points, vector<vector<int>>& queries) {
4+
vector<int> ans;
5+
for (auto& query : queries) {
6+
int x0 = query[0], y0 = query[1], r = query[2];
7+
int count = 0;
8+
for (auto& point : points) {
9+
int x = point[0], y = point[1];
10+
int dx = x - x0, dy = y - y0;
11+
if (dx * dx + dy * dy <= r * r) {
12+
++count;
13+
}
14+
}
15+
ans.push_back(count);
16+
}
17+
return ans;
18+
}
19+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
func countPoints(points [][]int, queries [][]int) []int {
2+
ans := make([]int, len(queries))
3+
for i, query := range queries {
4+
x0, y0, r := query[0], query[1], query[2]
5+
for _, point := range points {
6+
x, y := point[0], point[1]
7+
dx, dy := x-x0, y-y0
8+
if dx*dx+dy*dy <= r*r {
9+
ans[i]++
10+
}
11+
}
12+
}
13+
return ans
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public int[] countPoints(int[][] points, int[][] queries) {
3+
int[] ans = new int[queries.length];
4+
int i = 0;
5+
for (int[] query : queries) {
6+
int x0 = query[0], y0 = query[1], r = query[2];
7+
for (int[] point : points) {
8+
int x = point[0], y = point[1];
9+
int dx = x - x0, dy = y - y0;
10+
if (dx * dx + dy * dy <= r * r) {
11+
++ans[i];
12+
}
13+
}
14+
++i;
15+
}
16+
return ans;
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def countPoints(self, points: List[List[int]], queries: List[List[int]]) -> List[int]:
3+
ans = []
4+
for x0, y0, r in queries:
5+
count = 0
6+
for x, y in points:
7+
dx, dy = x - x0, y - y0
8+
if dx * dx + dy * dy <= r * r:
9+
count += 1
10+
ans.append(count)
11+
return ans

0 commit comments

Comments
 (0)