1237. 回旋镖的数量
在平面中给定n个点,每一对点都是不同的,“回旋镖”是一个点的的元组 (i, j, k),其中 i和 j 之间的距离与i和k之间的距离相同 (元组的顺序是重要的)。
找到回旋镖的数量。 您可以假设n最多为500并且点的坐标都在 [-10000, 10000] (包括)范围内。
样例
输入:
[[0,0],[1,0],[2,0]]
输出:
2
说明:
两个回旋镖是[[1,0], [0,0], [2,0]]和[[1,0], [2,0], [0,0]]
public class Solution {
/**
* @param points: a 2D array
* @return: the number of boomerangs
*/
public int numberOfBoomerangs(int[][] points) {
// Write your code here
int result = 0;
for (int i = 0; i < points.length; i++) {
ArrayList<Double> hashSet = new ArrayList<>();
for (int j = 0; j < points.length; j++) {
if (j == i) continue;
double point = (points[j][0] - points[i][0]) * (points[j][0] - points[i][0]) +
(points[j][1] - points[i][1]) * (points[j][1] - points[i][1]);
for (int k = 0; k < hashSet.size(); k++) {
if (hashSet.get(k) == point) {
result += 2;
}
}
hashSet.add(point);
}
}
return result;
}
}