Number 447- boomerang
For n-tuples over a given plane, "boomerang" is represented by point for different points (i, j, k)
, where the distance between the distance between the i and j and k and i are equal (taking into account the sequence of tuples) .
Find the number of all boomerang. You can assume a maximum of n 500 , coordinates of all points in the closed interval [-10000, 10000] in.
Example:
输入:
[[0,0],[1,0],[2,0]]
输出:
2
解释:
两个回旋镖为 [[1,0],[0,0],[2,0]] 和 [[1,0],[2,0],[0,0]]
Source: stay button (LeetCode)
link: https://leetcode-cn.com/problems/number-of-boomerangs
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.
public int numberOfBoomerangs(int[][] points) {
int res = 0;
Map<Integer, Integer> map = new HashMap<>();
for(int i = 0; i < points.length; i++) {
map.clear();
for(int j = 0; j < points.length; j++) {
if(i == j) {
continue;
}
int d = getDistance(points[i], points[j]);
int cnt = map.getOrDefault(d, 0);
res += cnt * 2;
map.put(d, cnt + 1);
}
}
return res;
}
private Integer getDistance(int[] point0, int[] point1) {
return (point0[0] - point1[0]) * (point0[0] - point1[0])
+ (point0[1] - point1[1]) * (point0[1] - point1[1]);
}