1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| class Solution { public int numberOfBoomerangs(int[][] points) { int count = 0;
for (int i = 0; i < points.length; i++) { Map<Integer, Integer> powerToCountMap = new HashMap<>();
for (int j = 0; j < points.length; j++) { if (i == j) { continue; } int xDistance = points[j][0] - points[i][0], yDistance = points[j][1] - points[i][1]; int powerDistance = xDistance * xDistance + yDistance * yDistance;
int sameDistanceCount = powerToCountMap.getOrDefault(powerDistance, 0); count += sameDistanceCount * 2; powerToCountMap.put(powerDistance, sameDistanceCount + 1); } }
return count; } }
|
以为有什么优于 O(n^2) 的解法,结果并没有。
References
447. Number of Boomerangs