447. Number of Boomerangs
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]]class Solution {
public int numberOfBoomerangs(int[][] points) {
if(points == null || points.length == 0 || points[0].length == 0) return 0;
int res = 0;
for (int[] p1 : points) {
Map<Integer, Integer> map = new HashMap<>();
for (int[] p2 : points) {
if (p1 == p2) continue;
int x = p1[0] - p2[0];
int y = p1[1] - p2[1];
int dist = x * x + y * y;
map.put(dist, map.getOrDefault(dist, 0) + 1);
}
for (int n : map.values()) res += n * (n - 1);
}
return res;
}
}Last updated