49
49
50
50
<!-- 这里可写通用的实现逻辑 -->
51
51
52
- 计数器实现。
52
+ ** 方法一:枚举 + 计数 **
53
53
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 ` 的长度。
55
59
56
60
<!-- tabs:start -->
57
61
63
67
class Solution :
64
68
def numberOfBoomerangs (self , points : List[List[int ]]) -> int :
65
69
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())
72
89
return ans
73
90
```
74
91
@@ -80,79 +97,145 @@ class Solution:
80
97
class Solution {
81
98
public int numberOfBoomerangs (int [][] points ) {
82
99
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);
91
106
}
92
107
}
93
- return ans;
108
+ return ans << 1 ;
94
109
}
95
110
}
96
111
```
97
112
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
+ }
111
126
}
127
+ return ans;
112
128
}
113
- return ans ;
114
129
}
115
130
```
116
131
117
- ### ** Go **
132
+ ### ** C++ **
118
133
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
+ };
133
150
```
134
151
135
- ### ** C++**
136
-
137
152
```cpp
138
153
class Solution {
139
154
public:
140
155
int numberOfBoomerangs(vector<vector<int>>& points) {
141
156
int ans = 0;
142
- for (const auto& p : points) {
157
+ for (auto& p1 : points) {
143
158
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]++;
146
162
}
147
- for (const auto& [ _ , v ] : cnt) {
148
- ans += v * (v - 1);
163
+ for (auto& [_, x ] : cnt) {
164
+ ans += x * (x - 1);
149
165
}
150
166
}
151
167
return ans;
152
168
}
153
169
};
154
170
```
155
171
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
+
156
239
### ** ...**
157
240
158
241
```
0 commit comments