C++ solution for number of boomerangs(89.03%) O(n2)


  • 0
    G
    class Solution {
    public:
        int numberOfBoomerangs(vector<pair<int, int>>& points) {
            int result=0;
            unordered_map<int,int> mp;
            for(int i=0;i<points.size();i++){
                int x1=points[i].first;
                int y1=points[i].second;
                for(int j=0;j<points.size();j++){
                    int x2=points[j].first;
                    int y2=points[j].second;
                    int dis=(y2-y1)*(y2-y1)+(x2-x1)*(x2-x1);
                    mp[dis]++;
                }
                for(unordered_map<int,int>::iterator it=mp.begin();it!=mp.end();it++)
                    if((*it).second>1)result+=((*it).second)*((*it).second-1);
                mp.clear();
            }
            return result;
        }    
    };

Log in to reply
 

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