描述
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: |
分析
输入是一些点的集合,首先给出一个回旋镖的概念,假设有 a, b, c 三个点,如果 ab 的距离等于 ac 的距离,则称 [a, b, c] 为回旋镖结构,a b c 的顺序是需要考虑的。求满足回旋镖概念的个数。
我们可以遍历所有节点,每个节点都可以作为 a,然后计算其余节点与 a 节点的距离,以距离进行分组,比如与 a 距离 x 的点有 n 个,那么这一组节点可以组成 n(n-1) 个回旋镖结构。
解决方案1(Javascript)
1 | /** |