码迷,mamicode.com
首页 > 其他好文 > 详细

447-回旋镖的数量

时间:2020-01-16 21:47:47      阅读:66      评论:0      收藏:0      [点我收藏+]

标签:https   put   ber   距离   nts   hashmap   联系   get   vat   

447-回旋镖的数量

给定平面上 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]]

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/number-of-boomerangs
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    public int numberOfBoomerangs(int[][] points) {
        int res = 0;
        Map<Integer, Integer> map = new HashMap<>();
        for(int i = 0; i < points.length; i++) {
            map.clear();
            for(int j = 0; j < points.length; j++) {
                if(i == j) {
                    continue;
                }
                int d = getDistance(points[i], points[j]);
                int cnt = map.getOrDefault(d, 0);
                res += cnt * 2;
                map.put(d, cnt + 1);
            }
        }
        return res;
    }

    private Integer getDistance(int[] point0, int[] point1) {
        return (point0[0] - point1[0]) * (point0[0] - point1[0])
                + (point0[1] - point1[1]) * (point0[1] - point1[1]);
    }

447-回旋镖的数量

标签:https   put   ber   距离   nts   hashmap   联系   get   vat   

原文地址:https://www.cnblogs.com/angelica-duhurica/p/12203362.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!