Skip to content

Commit 60bc898

Browse files
authored
feat: add solutions to lc problem: No.0447 (doocs#2196)
No.0447.Number of Boomerangs
1 parent c698627 commit 60bc898

File tree

7 files changed

+332
-161
lines changed

7 files changed

+332
-161
lines changed

solution/0400-0499/0447.Number of Boomerangs/README.md

+136-53
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,13 @@
4949

5050
<!-- 这里可写通用的实现逻辑 -->
5151

52-
计数器实现。
52+
**方法一:枚举 + 计数**
5353

54-
对于每个点,计算其他点到该点的距离,然后按照距离进行分组计数。对每个组中的点进行两两排列组合(A n 取 2,即 `n * (n - 1))`)计数即可。
54+
我们可以枚举 `points` 中的每个点作为回旋镖的点 $i$,然后用一个哈希表 $cnt$ 记录其他点到 $i$ 的距离出现的次数。
55+
56+
如果有 $x$ 个点到 $i$ 的距离相等,那么我们可以任选其中 $2$ 个点作为回旋镖的 $j$ 和 $k$,方案数为 $A_x^2 = x \times (x - 1)$。因此,我们对哈希表中的每个值 $x$,都计算并累加 $A_x^2$,就可以得到满足题目要求的回旋镖数量之和。
57+
58+
时间复杂度 $O(n^2)$,空间复杂度 $O(n)$。其中 $n$ 是数组 `points` 的长度。
5559

5660
<!-- tabs:start -->
5761

@@ -63,12 +67,25 @@
6367
class Solution:
6468
def numberOfBoomerangs(self, points: List[List[int]]) -> int:
6569
ans = 0
66-
for p in points:
67-
counter = Counter()
68-
for q in points:
69-
distance = (p[0] - q[0]) * (p[0] - q[0]) + (p[1] - q[1]) * (p[1] - q[1])
70-
counter[distance] += 1
71-
ans += sum([val * (val - 1) for val in counter.values()])
70+
for p1 in points:
71+
cnt = Counter()
72+
for p2 in points:
73+
d = dist(p1, p2)
74+
ans += cnt[d]
75+
cnt[d] += 1
76+
return ans << 1
77+
```
78+
79+
```python
80+
class Solution:
81+
def numberOfBoomerangs(self, points: List[List[int]]) -> int:
82+
ans = 0
83+
for p1 in points:
84+
cnt = Counter()
85+
for p2 in points:
86+
d = dist(p1, p2)
87+
cnt[d] += 1
88+
ans += sum(x * (x - 1) for x in cnt.values())
7289
return ans
7390
```
7491

@@ -80,79 +97,145 @@ class Solution:
8097
class Solution {
8198
public int numberOfBoomerangs(int[][] points) {
8299
int ans = 0;
83-
for (int[] p : points) {
84-
Map<Integer, Integer> counter = new HashMap<>();
85-
for (int[] q : points) {
86-
int distance = (p[0] - q[0]) * (p[0] - q[0]) + (p[1] - q[1]) * (p[1] - q[1]);
87-
counter.put(distance, counter.getOrDefault(distance, 0) + 1);
88-
}
89-
for (int val : counter.values()) {
90-
ans += val * (val - 1);
100+
for (int[] p1 : points) {
101+
Map<Integer, Integer> cnt = new HashMap<>();
102+
for (int[] p2 : points) {
103+
int d = (p1[0] - p2[0]) * (p1[0] - p2[0]) + (p1[1] - p2[1]) * (p1[1] - p2[1]);
104+
ans += cnt.getOrDefault(d, 0);
105+
cnt.merge(d, 1, Integer::sum);
91106
}
92107
}
93-
return ans;
108+
return ans << 1;
94109
}
95110
}
96111
```
97112

98-
### **TypeScript**
99-
100-
```ts
101-
function numberOfBoomerangs(points: number[][]): number {
102-
let ans = 0;
103-
for (let p1 of points) {
104-
let hashMap: Map<number, number> = new Map();
105-
for (let p2 of points) {
106-
const distance = (p1[0] - p2[0]) ** 2 + (p1[1] - p2[1]) ** 2;
107-
hashMap.set(distance, (hashMap.get(distance) || 0) + 1);
108-
}
109-
for (let [, v] of [...hashMap]) {
110-
ans += v * (v - 1);
113+
```java
114+
class Solution {
115+
public int numberOfBoomerangs(int[][] points) {
116+
int ans = 0;
117+
for (int[] p1 : points) {
118+
Map<Integer, Integer> cnt = new HashMap<>();
119+
for (int[] p2 : points) {
120+
int d = (p1[0] - p2[0]) * (p1[0] - p2[0]) + (p1[1] - p2[1]) * (p1[1] - p2[1]);
121+
cnt.merge(d, 1, Integer::sum);
122+
}
123+
for (int x : cnt.values()) {
124+
ans += x * (x - 1);
125+
}
111126
}
127+
return ans;
112128
}
113-
return ans;
114129
}
115130
```
116131

117-
### **Go**
132+
### **C++**
118133

119-
```go
120-
func numberOfBoomerangs(points [][]int) int {
121-
ans := 0
122-
for _, p := range points {
123-
cnt := make(map[int]int)
124-
for _, q := range points {
125-
cnt[(p[0]-q[0])*(p[0]-q[0])+(p[1]-q[1])*(p[1]-q[1])]++
126-
}
127-
for _, v := range cnt {
128-
ans += v * (v - 1)
129-
}
130-
}
131-
return ans
132-
}
134+
```cpp
135+
class Solution {
136+
public:
137+
int numberOfBoomerangs(vector<vector<int>>& points) {
138+
int ans = 0;
139+
for (auto& p1 : points) {
140+
unordered_map<int, int> cnt;
141+
for (auto& p2 : points) {
142+
int d = (p1[0] - p2[0]) * (p1[0] - p2[0]) + (p1[1] - p2[1]) * (p1[1] - p2[1]);
143+
ans += cnt[d];
144+
cnt[d]++;
145+
}
146+
}
147+
return ans << 1;
148+
}
149+
};
133150
```
134151
135-
### **C++**
136-
137152
```cpp
138153
class Solution {
139154
public:
140155
int numberOfBoomerangs(vector<vector<int>>& points) {
141156
int ans = 0;
142-
for (const auto& p : points) {
157+
for (auto& p1 : points) {
143158
unordered_map<int, int> cnt;
144-
for (const auto& q : points) {
145-
++cnt[(p[0] - q[0]) * (p[0] - q[0]) + (p[1] - q[1]) * (p[1] - q[1])];
159+
for (auto& p2 : points) {
160+
int d = (p1[0] - p2[0]) * (p1[0] - p2[0]) + (p1[1] - p2[1]) * (p1[1] - p2[1]);
161+
cnt[d]++;
146162
}
147-
for (const auto& [_, v] : cnt) {
148-
ans += v * (v - 1);
163+
for (auto& [_, x] : cnt) {
164+
ans += x * (x - 1);
149165
}
150166
}
151167
return ans;
152168
}
153169
};
154170
```
155171

172+
### **Go**
173+
174+
```go
175+
func numberOfBoomerangs(points [][]int) (ans int) {
176+
for _, p1 := range points {
177+
cnt := map[int]int{}
178+
for _, p2 := range points {
179+
d := (p1[0]-p2[0])*(p1[0]-p2[0]) + (p1[1]-p2[1])*(p1[1]-p2[1])
180+
ans += cnt[d]
181+
cnt[d]++
182+
}
183+
}
184+
ans <<= 1
185+
return
186+
}
187+
```
188+
189+
```go
190+
func numberOfBoomerangs(points [][]int) (ans int) {
191+
for _, p1 := range points {
192+
cnt := map[int]int{}
193+
for _, p2 := range points {
194+
d := (p1[0]-p2[0])*(p1[0]-p2[0]) + (p1[1]-p2[1])*(p1[1]-p2[1])
195+
cnt[d]++
196+
}
197+
for _, x := range cnt {
198+
ans += x * (x - 1)
199+
}
200+
}
201+
return
202+
}
203+
```
204+
205+
### **TypeScript**
206+
207+
```ts
208+
function numberOfBoomerangs(points: number[][]): number {
209+
let ans = 0;
210+
for (const [x1, y1] of points) {
211+
const cnt: Map<number, number> = new Map();
212+
for (const [x2, y2] of points) {
213+
const d = (x1 - x2) ** 2 + (y1 - y2) ** 2;
214+
ans += cnt.get(d) || 0;
215+
cnt.set(d, (cnt.get(d) || 0) + 1);
216+
}
217+
}
218+
return ans << 1;
219+
}
220+
```
221+
222+
```ts
223+
function numberOfBoomerangs(points: number[][]): number {
224+
let ans = 0;
225+
for (const [x1, y1] of points) {
226+
const cnt: Map<number, number> = new Map();
227+
for (const [x2, y2] of points) {
228+
const d = (x1 - x2) ** 2 + (y1 - y2) ** 2;
229+
cnt.set(d, (cnt.get(d) || 0) + 1);
230+
}
231+
for (const [_, x] of cnt) {
232+
ans += x * (x - 1);
233+
}
234+
}
235+
return ans;
236+
}
237+
```
238+
156239
### **...**
157240

158241
```

0 commit comments

Comments
 (0)