LeetCode刷题实战447:回旋镖的数量
共 1137字,需浏览 3分钟
·
2021-11-26 19:44
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.
示例
示例 1:
输入:points = [[0,0],[1,0],[2,0]]
输出:2
解释:两个回旋镖为 [[1,0],[0,0],[2,0]] 和 [[1,0],[2,0],[0,0]]
示例 2:
输入:points = [[1,1],[2,2],[3,3]]
输出:2
示例 3:
输入:points = [[1,1]]
输出:0
解题
class Solution {
public static int numberOfBoomerangs(int[][] points) {
int len=points.length;
int res=0;
HashMaphashMap=new HashMap ();
for(int i=0;ifor(int j=0;j if(i!=j){
int x=points[i][0]-points[j][0];
int y=points[i][1]-points[j][1];
int dist=x*x+y*y;
if(hashMap.containsKey(dist)){
hashMap.put(dist,hashMap.get(dist)+1);
}else{
hashMap.put(dist,1);
}
}
}
for(Integer count:hashMap.values()){
res+=count*(count-1);
}
hashMap.clear();
}
return res;
}
}
LeetCode刷题实战446:等差数列划分 II - 子序列