程式語言 - LeetCode - C++ - 447. Number of Boomerangs



題目:


解答:

class Solution {
public:
    int numberOfBoomerangs(vector<vector<int>>& points) {
        int ans = 0;
        int n = points.size();

        for (int i = 0; i < n; ++i) {
            unordered_map<int, int> mp;

            for (int j = 0; j < n; ++j) {
                if (i == j) {
                    continue;
                }

                int dx = points[i][0] - points[j][0];
                int dy = points[i][1] - points[j][1];
                int d = dx * dx + dy * dy;
                mp[d] += 1;
            }

            for (auto& [dist, num] : mp) {
                ans += (num * (num - 1));
            }
        }

        return ans;
    }
};