Number of Boomerangs LeetCode solution in Java

1
class Solution {
2
public int numberOfBoomerangs(int[][] points) {
3

4
int answer = 0;
5

6
for (int p = 0; p < points.length; p++) {
7

8
int[] i = points[p];
9

10
HashMap<Double, Integer> hm = new HashMap<Double, Integer>();
11

12
for (int q = 0; q < points.length; q++) {
13

14
if (q == p) {
15
continue;
16
}
17

18
int[] j = points[q];
19

20
double distance = Math.sqrt(Math.pow(j[0] - i[0], 2) + Math.pow(j[1] - i[1], 2));
21

22
if (distance > 0) {
23
if (hm.containsKey(distance)) {
24
hm.put(distance, hm.get(distance) + 1);
25
} else {
26
hm.put(distance, 1);
27
}
28
}
29
}
30

31
for (Double dist : hm.keySet()) {
32
int occ = hm.get(dist);
33
if (occ > 1) {
34
answer = answer + ((occ) * (occ - 1));
35
}
36
}
37
}
38

39
return answer;
40
}
41
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0