# [LeetCode] 447. Number of Boomerangs

You are given `n` `points` in the plane that are all distinct, where `points[i] = [xi, yi]`. 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).

Return the number of boomerangs.

Example 1:

```Input: points = [[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]].
```

Example 2:

```Input: points = [[1,1],[2,2],[3,3]]
Output: 2
```

Example 3:

```Input: points = [[1,1]]
Output: 0```

Constraints:

• `n == points.length`
• `1 <= n <= 500`
• `points[i].length == 2`
• `-104 <= xi, yi <= 104`
• All the points are unique.

Java实现

``` 1 class Solution {
2     public int numberOfBoomerangs(int[][] points) {
3         int res = 0;
4         for (int i = 0; i < points.length; i++) {
5             HashMap<Integer, Integer> map = new HashMap<>();
6             for (int j = 0; j < points.length; j++) {
7                 if (i == j) {
8                     continue;
9                 }
10                 int dis = helper(points[i], points[j]);
11                 map.put(dis, map.getOrDefault(dis, 0) + 1);
12             }
13             for (int key : map.keySet()) {
14                 int c = map.get(key);
15                 res += c * (c - 1);
16             }
17         }
18         return res;
19     }
20
21     private int helper(int[] point1, int[] point2) {
22         int x = point1[0] - point2[0];
23         int y = point1[1] - point2[1];
24         return x * x + y * y;
25     }
26 }```

356. Line Reflection

447. Number of Boomerangs

LeetCode 题目总结

[LeetCode] 447. Number of Boomerangs

(0)
(0)