A simple, easy Java solution.


  • 0
    C

    I have to say stream in Java8 is a good thing!

    public class Solution {
        public int numberOfBoomerangs(int[][] points) {
            int ans=0, n=points.length;
            for (int i=0;i<n;i++) {
                HashMap<Integer, Integer> map=new HashMap<>();
                for (int j=0;j<n;j++) {
                    int distance=(points[i][0]-points[j][0])*(points[i][0]-points[j][0])+(points[i][1]-points[j][1])*(points[i][1]-points[j][1]);
                    map.put(distance, map.getOrDefault(distance, 0)+1);
                }
                ans+=map.values().stream().mapToInt(x -> x*(x-1)).sum();
            }
            return ans;
        }
    }
    

Log in to reply
 

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