congr / world

2 stars 1 forks source link

LeetCode : 447. Number of Boomerangs #452

Closed congr closed 5 years ago

congr commented 5 years ago

https://leetcode.com/problems/number-of-boomerangs/

image

congr commented 5 years ago
class Solution {
    public int numberOfBoomerangs(int[][] points) {
        Map<Integer, Integer> map = new HashMap();
        int res = 0;

        for (int i = 0; i< points.length; i++) {
            for (int j = 0; j<points.length; j++) {
                if (i==j) continue;
                int[] p = points[i], q = points[j];
                map.merge(getDist(p, q), 1, Integer::sum);
            }

            for (int n : map.values()) 
                res += n * (n-1); // ????

            map.clear(); // !!!
        }    

        return res;
    }

    int getDist(int[] p, int [] q) {
        int dy = p[0] - q[0];
        int dx = p[1] - q[1];

        return dx*dx + dy*dy;
    }
}