-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberofBoomerangs.java
More file actions
52 lines (49 loc) · 1.52 KB
/
NumberofBoomerangs.java
File metadata and controls
52 lines (49 loc) · 1.52 KB
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
43
44
45
46
47
48
49
50
51
52
package easy;
import java.util.HashMap;
import java.util.Map;
/**
* ClassName: NumberofBoomerangs.java
* Author: chenyiAlone
* Create Time: 2019/8/30 22:34
* Description: No.447 Number of Boomerangs
*
* Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of points (i, j, k) such that the distance between i and j equals the distance between i and k (the order of the tuple matters).
*
* Find the number of boomerangs. You may assume that n will be at most 500 and coordinates of points are all in the range [-10000, 10000] (inclusive).
*
* Example:
*
* Input:
* [[0,0],[1,0],[2,0]]
*
* Output:
* 2
*
* Explanation:
* The two boomerangs are [[1,0],[0,0],[2,0]] and [[1,0],[2,0],[0,0]]
*
*/
public class NumberofBoomerangs {
private int dist(int[] a, int[] b) {
return (a[0] - b[0]) * (a[0] - b[0]) + (a[1] - b[1]) * (a[1] - b[1]);
}
public int numberOfBoomerangs(int[][] points) {
int ret = 0;
for (int i = 0; i < points.length; i++) {
Map<Integer, Integer> map = new HashMap<>();
for (int j = 0; j < points.length; j++) {
if (i != j) {
int key = dist(points[i], points[j]);
if (!map.containsKey(key)) {
map.put(key, 0);
}
map.put(key, map.get(key) + 1);
}
}
for (int d : map.values()) {
ret += d * (d - 1);
}
}
return ret;
}
}