第44天(中等题 数据结构)

打卡第四十四天
2道中等题
image

题目:
image

思路:以每个点为中心,统计与它距离相同的点对数量

代码:

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;
    }
};

image

耗时≈一小时 明天继续

posted @ 2025-12-04 00:34  Wy0518  阅读(1)  评论(0)    收藏  举报