-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathNumber of Boomerangs.java
42 lines (30 loc) · 1.17 KB
/
Number of Boomerangs.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class Solution {
public int numberOfBoomerangs(int[][] points) {
int answer = 0;
for (int p=0; p<points.length;p++) {
int[] i = points[p];
HashMap<Double, Integer> hm = new HashMap<Double, Integer>();
for (int q=0;q<points.length;q++) {
if (q==p) {
continue;
}
int[] j = points[q];
double distance = Math.sqrt(Math.pow(j[0]-i[0], 2) + Math.pow(j[1]-i[1], 2));
if (distance > 0) {
if (hm.containsKey(distance)) {
hm.put(distance, hm.get(distance) + 1);
} else {
hm.put(distance, 1);
}
}
}
for (Double dist : hm.keySet()) {
int occ = hm.get(dist);
if (occ > 1) {
answer = answer + ((occ) * (occ - 1));
}
}
}
return answer;
}
}