## 题目描述

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.

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]]``````

## 解法一

### Python

``````class Solution:
def numberOfBoomerangs(self, points):
"""
:type points: List[List[int]]
:rtype: int
"""
result = 0
for point_a in points:
distances = {}
for point_b in points:
distance = (point_a[0] - point_b[0]) ** 2 + (point_a[1] - point_b[1]) ** 2
distances[distance] = distances.get(distance, 0) + 1
result += sum(item * (item - 1) for item in distances.values())
return result``````

### Java

``````class Solution {
public int numberOfBoomerangs(int[][] points) {
int result = 0;
for (int[] pointA : points) {
Map<Integer, Integer> distances = new HashMap<>();
for (int[] pointB : points) {
int distance = (pointA[0] - pointB[0]) * (pointA[0] - pointB[0]) + (pointA[1] - pointB[1]) * (pointA[1] - pointB[1]);
distances.put(distance, distances.getOrDefault(distance, 0) + 1);
}
for (int item : distances.values()) {
result += item * (item - 1);
}
}
return result;
}
}``````

### C++

``````class Solution {
public:
int numberOfBoomerangs(vector<pair<int, int>> &points) {
int result = 0;
for (auto &pointA : points) {
map<int, int> distances;
for (auto &pointB : points) {
int distance = (pointA.first - pointB.first) * (pointA.first - pointB.first) +
(pointA.second - pointB.second) * (pointA.second - pointB.second);
distances[distance]++;
}
for (auto &item : distances) {
result += item.second * (item.second - 1);
}
}
return result;
}
};``````