查找表类算法//回旋镖的数量

给定平面上 n 对不同的点,“回旋镖” 是由点表示的元组 (i, j, k) ,其中 i 和 j 之间的距离和 i 和 k 之间的距离相等(需要考虑元组的顺序)。

找到所有回旋镖的数量。你可以假设 n 最大为 500,所有点的坐标在闭区间 [-10000, 10000] 中。

示例:

输入:
[[0,0],[1,0],[2,0]]

输出:
2

解释:
两个回旋镖为 [[1,0],[0,0],[2,0]][[1,0],[2,0],[0,0]]
class Solution {
    private int getDistance(int[] a, int[] b){
        int dx = a[0] - b[0];
        int dy = a[1] - b[1];
        return dx*dx+dy*dy;
    }
    public int numberOfBoomerangs(int[][] points) {
        if(points == null)
            return 0;
        int res = 0;
        for(int i = 0; i < points.length; i++){
            Map<Integer,Integer> disCount = new HashMap<>();
            for(int j = 0; j < points.length; j++){
                if(i == j)
                    continue;
                int distance = getDistance(points[i], points[j]);
                int count = disCount.getOrDefault(distance,0);
                disCount.put(distance,count+1);
            }
            for(int count:disCount.values()){
            res += count*(count-1);
            }
        }
        return res;
    }
}

getOrDefault(): 当Map集合中有这个key时,就使用这个key值,如果没有就使用默认值defaultValue

class Solution {
public:
    int numberOfBoomerangs(vector<pair<int, int>>& points) {
        int res = 0;
        unordered_map<double,int> Hash;
        for(pair<int,int>p1:points){
            Hash.clear();
            for(pair<int,int>p2:points){
                if(p1 == p2)
                    continue;
                Hash[hypot(p1.first-p2.first,p1.second-p2.second)]++;
            }
            for(auto val:Hash){
                if(val.second>1)
                    res+=val.second*(val.second-1);
            }
        }
        return res;
    }
};

hypot(x,y):return x*x+y*y

posted @ 2018-11-08 18:51  strawqqhat  阅读(96)  评论(0编辑  收藏  举报
#home h1{ font-size:45px; } body{ background-image: url("放你的背景图链接"); background-position: initial; background-size: cover; background-repeat: no-repeat; background-attachment: fixed; background-origin: initial; background-clip: initial; height:100%; width:100%; } #home{ opacity:0.7; } .wall{ position: fixed; top: 0; left: 0; bottom: 0; right: 0; } div#midground{ background: url("https://i.postimg.cc/PP5GtGtM/midground.png"); z-index: -1; -webkit-animation: cc 200s linear infinite; -moz-animation: cc 200s linear infinite; -o-animation: cc 200s linear infinite; animation: cc 200s linear infinite; } div#foreground{ background: url("https://i.postimg.cc/z3jZZD1B/foreground.png"); z-index: -2; -webkit-animation: cc 253s linear infinite; -o-animation: cc 253s linear infinite; -moz-animation: cc 253s linear infinite; animation: cc 253s linear infinite; } div#top{ background: url("https://i.postimg.cc/PP5GtGtM/midground.png"); z-index: -4; -webkit-animation: da 200s linear infinite; -o-animation: da 200s linear infinite; animation: da 200s linear infinite; } @-webkit-keyframes cc { from{ background-position: 0 0; transform: translateY(10px); } to{ background-position: 600% 0; } } @-o-keyframes cc { from{ background-position: 0 0; transform: translateY(10px); } to{ background-position: 600% 0; } } @-moz-keyframes cc { from{ background-position: 0 0; transform: translateY(10px); } to{ background-position: 600% 0; } } @keyframes cc { 0%{ background-position: 0 0; } 100%{ background-position: 600% 0; } } @keyframes da { 0%{ background-position: 0 0; } 100%{ background-position: 0 600%; } } @-webkit-keyframes da { 0%{ background-position: 0 0; } 100%{ background-position: 0 600%; } } @-moz-keyframes da { 0%{ background-position: 0 0; } 100%{ background-position: 0 600%; } } @-ms-keyframes da { 0%{ background-position: 0 0; } 100%{ background-position: 0 600%; } }