C++ solution using map<int, int>


  • 0
    M
    class Solution {
    public:
        int numberOfBoomerangs(vector<pair<int, int>>& points) {
            int pt_size = points.size();
            int count = 0;
            for(int i = 0; i < pt_size; ++i){
                int dis = 0;
                map<int, int> my_map;
                for(int j = 0; j < pt_size; ++j){
                    if(points[i] != points[j]){
                        dis = pow(points[i].first - points[j].first, 2) + pow(points[i].second - points[j].second, 2);
                        if(my_map.find(dis) == my_map.end()){
                            my_map.insert(pair<int, int>(dis, 1));
                        }else{
                            (my_map.find(dis)->second)++;
                        }
                    }
                }
                for(auto it = my_map.begin(); it != my_map.end(); ++it){
                    int num = it->second;
                    if(num >= 2){
                        count += num * (num - 1);
                    }
                }
            }
            return count;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.