Title:Number of Boomerangs 447
Difficulty:Easy
原题leetcode地址: https://leetcode.com/problems/number-of-boomerangs/
1. 采用map
时间复杂度:O(n^2),嵌套循环。
空间复杂度:O(n),申请Map。
/**
* 采用Map
* @param points
* @return
*/
public static int numberOfBoomerangs(int[][] points) {
int count = 0;
for (int i = 0; i < points.length; i++) {
Map<Integer, Integer> map = new HashMap<>();
for (int j = 0; j < points.length; j++) {
int x = points[i][0] - points[j][0];
int y = points[i][1] - points[j][1];
int d = x * x + y * y;
if (!map.containsKey(d)) {
map.put(d, 0);
}
count += map.get(d) * 2;
map.put(d, map.get(d) + 1);
}
}
return count;
}