第44天(中等题 数据结构)
打卡第四十四天
2道中等题

题目:

思路:以每个点为中心,统计与它距离相同的点对数量
代码:
class Solution {
public:
int numberOfBoomerangs(vector<vector<int>>& points) {
int ans = 0;
int n = points.size();
for(int i = 0;i < n;i++){// 外层选择中心点 i
unordered_map<int,int> cnt;
for(int j = 0;j < n;j++){// 内层:计算到所有点的距离
if(i==j){
continue;
}
else{
int x = points[i][0]-points[j][0];
int y = points[i][1]-points[j][1];
cnt[x * x + y * y]++;//计算距离
}
}
for(auto p :cnt){
ans += p.second * (p.second - 1);//p.second 指该距离出现的次数,排列数顺序有影响
}
}
return ans;
}
};

耗时≈一小时 明天继续

浙公网安备 33010602011771号