Clear Java Solution, 116 ms beats 99%


  • 0
    K
    class Solution {
        public int numberOfBoomerangs(int[][] points) {
            if (points == null || points.length == 0) {
                return 0;
            }
            
            Map<Integer, Integer> map = new HashMap<>();
            int count = 0;
            
            for (int[] p1 : points) {
                for (int[] p2 : points) {
                    if (p1 == p2) {
                        continue;
                    }
                    
                    int x = p1[0] - p2[0];
                    int y = p1[1] - p2[1];
                    int dis = x*x + y*y;
                    
                    int n = map.getOrDefault(dis, 0);
                    
                    map.put(dis, n + 1);
                    count += n;
                }
                  
                map.clear();
            }
            
            return count * 2;
        }
    }
    

Log in to reply
 

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