Solution with a little Java8 feature


  • 0
    L
    public class Solution {
        public int numberOfBoomerangs(int[][] points) {
            int counter = 0;
            for(int i = 0; i < points.length; i++) {
                Map<Integer, Integer> map = new HashMap<>();
                for(int j = 0; j < points.length; j++) {
                    int distance = getDistance(points[i], points[j]);
                    map.put(distance, map.getOrDefault(distance, 0) + 1);
                }
                counter += map.values().stream()
                            .map(num -> num*(num-1))
                            .reduce(0, (sum, num) -> (sum + num));
            }
            return counter;
        }
        
        private int getDistance(int[] p1, int[] p2) {
            int dx = p1[0] - p2[0];
            int dy = p1[1] - p2[1];
            return dx*dx + dy*dy;
        }
    }
    

    However the speed is not ideal.


Log in to reply
 

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