[leetcode]447. Number of Boomerangs

[leetcode]447. Number of Boomerangs


Analysis

pre做完一身轻松~—— [嘻嘻~]

Given n points in the plane that are all pairwise distinct, 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).
给出点的坐标,找出距离相等的节点对(i, j)和(i, k)。

Implement

class Solution {
public:
    int numberOfBoomerangs(vector<pair<int, int>>& points) {
        int res = 0;
        for(auto &p1:points){
            map<int, int> dis;
            for(auto &p2:points){
                int len = (p1.first - p2.first) * (p1.first - p2.first) + (p1.second - p2.second) * (p1.second - p2.second);
                dis[len]++;
            }
            for(auto &it:dis)
                res += it.second * (it.second - 1);
        }
        return res;
    }
};

猜你喜欢

转载自blog.csdn.net/weixin_32135877/article/details/80802671