0447. Number of Boomerangs

# 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 1:

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

``````

## 代码 #

``````
package leetcode

func numberOfBoomerangs(points [][]int) int {
res := 0
for i := 0; i < len(points); i++ {
record := make(map[int]int, len(points))
for j := 0; j < len(points); j++ {
if j != i {
record[dis(points[i], points[j])]++
}
}
for _, r := range record {
res += r * (r - 1)
}
}
return res
}

func dis(pa, pb []int) int {
return (pa[0]-pb[0])*(pa[0]-pb[0]) + (pa[1]-pb[1])*(pa[1]-pb[1])
}

``````

Apr 8, 2023