Java best solution beats 97% solutions.


  • 0
    S
    public int numberOfBoomerangs(int[][] points) {
            int result =0;
            HashMap<Double,Double> map = new HashMap<>();
            double dist=0,temp=0;
            for(int i=0;i<points.length;i++) {
                map.clear();
                for(int j=0;j<points.length;j++) {
                    if(i==j)
                    continue;
                    dist = Math.pow(points[i][0]-points[j][0],2)+Math.pow(points[i][1]-points[j][1],2);
                    if(map.containsKey(dist)) {
                        temp = map.get(dist);
                        result +=temp*2;
                        map.put(dist,temp+1);
                    } else {
                        map.put(dist,1d);         
                    }
                }
            }
            
            return (int)result;}

Log in to reply
 

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