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:12345678Input:[[0,0],[1,0],[2,0]]Output:2Explanation:The two boomerangs are [[1,0],[0,0],[2,0]] and [[1,0],[2,0],[0,0]]
直接的做法:
- 针对每一个点, 计算其与其他点的距离, 存放在HashMap里, key 为距离, value 为点的数量
- 现在我们有了很多
pair. 根据排列的计算公式, 距离当前点为distance的两个点的排列的数量为 counter * (counter - 1)
代码如下:
|
|