Python O(n^2) HashMap solution


  • 0
    class Solution(object):
        def numberOfBoomerangs(self, points):
            """
            :type points: List[List[int]]
            :rtype: int
            """
    
            d = {}
            ans = 0
            for point in points:
                p =str(point)
                d[p] = {}
                for point2 in points:
                    dis = (point[0]-point2[0])**2 + (point[1]-point2[1])**2
                    d[p][dis] =d[p].get(dis, 0) + 1
                for dis in d[p]:
                    if d[p][dis]>1:
                        ans += d[p][dis]*(d[p][dis]-1)
            return ans
    

Log in to reply
 

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