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:
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]]
解法1:O(N^2)
排列组合的题。假设有a,b,c。如果bc和a的距离相等,那么可以产生abc或者acb两种排列。如果是bcd和a的距离相等,那么可以产生6种排列。对于有n个数和a相等的情况,总共的个数是n*(n-1)。
那么问题就转化为,对于每一个pair作为a,计算每一种距离的pair的个数,然后再把总和相加就是答案。
C++
Java