leetcode_447_回旋镖的数量

给定平面上 n 对不同的点,“回旋镖” 是由点表示的元组 (i, j, k) ,其中 i 和 j 之间的距离和 i 和 k 之间的距离相等(需要考虑元组的顺序)。

找到所有回旋镖的数量。你可以假设 n 最大为 500,所有点的坐标在闭区间 [-10000, 10000] 中。

示例:

输入:
[[0,0],[1,0],[2,0]]

输出:
2

解释:
两个回旋镖为 [[1,0],[0,0],[2,0]][[1,0],[2,0],[0,0]]

class Solution {
public:
    int numberOfBoomerangs(vector<pair<int, int>>& points) {
        int len = points.size(), res = 0;
        unordered_map<int, int> m;
        
        for (int i = 0; i < len; i++) {
            for (int j = 0; j < len; j++) {
                int x = points[i].first - points[j].first;
                int y = points[i].second - points[j].second;
                m[x * x + y * y]++;  //记录下每个点到其中一个点的距离数
            }
            
            unordered_map<int, int> :: iterator it;
            for (it = m.begin(); it != m.end(); it++) {
                int tmp = it->second;
                res += tmp * (tmp - 1);  
            }
            m.clear();
        }
        
        return res;
    }
};
若在一组点集{a, b, c, d, ...}中,以点a为一个端点,与dis(a, b)相等的点存在n个(包含点b),那么在这n个点中任意选出两个点与点a构成三元组,则有n(n - 1) / 2种情况。但因为三元组[a, b, c]与三元组[a, c, b]并不相同,所以实际为排列问题,答案为n(n - 1)。

猜你喜欢

转载自blog.csdn.net/snow_jie/article/details/80876929