My Java O(N^2) solution, 107ms, beasts 99% submission

  • 0

    Don't waste time on calculating square root!

    public int numberOfBoomerangs(int[][] points) {
            HashMap<Double, Integer> map = new HashMap<>();
            int count=0;
            for(int i=0;i<points.length;i++){
                int cx=points[i][0];
                int cy=points[i][1];
                for(int j=0;j<points.length;j++){
                    double d=Math.pow(points[j][0]-cx,2)+Math.pow(points[j][1]-cy,2);
                    if(map.containsKey(d)) count+= 2*map.put(d,map.get(d)+1);
                    else map.put(d,1);
            return count;

Log in to reply

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