C++ solution, runtime 995ms, beat 80.90%


  • 0
    L
    class Solution {
    public:
        int numberOfBoomerangs(vector<pair<int, int>>& points) {
            int num=0;
            int len=points.size();
            long disMatrix[500][500]={0};
            unordered_map<long,int> myM;
            
            for(int i=0;i<len-1;++i)
            {
                for(int j=i+1;j<len;++j)
                {
                    disMatrix[i][j]=(points[i].first-points[j].first)*(points[i].first-points[j].first)+(points[i].second-points[j].second)*(points[i].second-points[j].second);
                    disMatrix[j][i]=disMatrix[i][j];
                }
            }
            
            for(int i=0;i<len;++i)
            {
                myM.clear();
                
                for(int j=0;j<len;++j)
                {
                    if(j==i)
                    {
                        continue;
                    }
                    
                    num+=myM[disMatrix[i][j]];
                    ++myM[disMatrix[i][j]];
                }
            }
            
            return num*2;
        }
    };
    

Log in to reply
 

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