Java/447. Number of Boomerangs 回旋镖的数量

题目


代码部分(148ms 89.76%)

class Solution {
    public int numberOfBoomerangs(int[][] points) {
        int result = 0;
        int dis = 0;
        int freq = 0;
        int tempRes = 0;
        HashMap<Integer, Integer> hashDis = new HashMap<Integer, Integer>();
        for(int i = 0; i < points.length; i++){
            for(int j = 0; j < points.length; j++){
                dis = (points[j][0] - points[i][0]) * (points[j][0] - points[i][0])
                    + (points[j][1] - points[i][1]) * (points[j][1] - points[i][1]);
                if(dis != 0){
                    if(hashDis.containsKey(dis)){
                        freq = hashDis.get(dis);
                        hashDis.put(dis, freq + 1);
                        tempRes += freq;
                    }else{
                        hashDis.put(dis, 1);
                    }
                }
            }
            result += tempRes * 2;
            tempRes = 0;
            hashDis.clear();
        }
        return result;
    }
}

猜你喜欢

转载自blog.csdn.net/qq_38959715/article/details/83034025