• LeetCode 447. Number of Boomerangs (回力标的数量)


    Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of points (i, j, k) such that the distance between iand j equals the distance between i and k (the order of the tuple matters).

    Find the number of boomerangs. You may assume that n will be at most 500 and coordinates of points are all in the range [-10000, 10000] (inclusive).

    Example:

    Input:
    [[0,0],[1,0],[2,0]]
    
    Output:
    2
    
    Explanation:
    The two boomerangs are [[1,0],[0,0],[2,0]] and [[1,0],[2,0],[0,0]]
    

    题目标签: Hash Table

      题目给了我们一个 点坐标的 array,让我们找到“回力标”的数量。

      首先我们看题目中的例子:

        0,0  1,0  2,0     这里 1,0 到 0,0 的距离 等于 到 2,0 的距离;

                根据题意,它有2种可能性  1,0  0,0  2,0   和 1,0  2,0  0,0

        那么如果有一个点,它到其他三个点的距离都一样,那么对于这个点,它有几种可能性呢?

          它的组合是 三个点: p1  p2  p3

          首先p1 都是它自己,不会变;

          p2 会从3个点中选一个;

          p3 会从选剩下的2个点中选一个;

          所以答案就是: 1 *  3  *  2  公式就是 1  *  n  *  (n-1)

      搞清楚这点以后,我们可以遍历每一个点:

        对于每一个点,把它与其他所有点之间的距离,计算之后当作 key 存入 map,它的出现次数当作 value 存入;

        如果一个距离的 value = 3, 说明 这个点 到3 个点的距离是一样的,所以只要遍历 value, 把所有的value * (value - 1) 进行累加。

        要注意的是,如果value = 1,说明点a 到点b = key,并没有点a 到 点c 这个c 存在,不需要加入累加,我们也不需要做什么,因为 1 * 0 = 0。

      还有一点就是 两点之间的距离公式,这里并没有开根号,因为并不影响结果,这样更简单方便。

    Java Solution:

    Runtime beats 79.05% 

    完成日期:06/06/2017

    关键词:HashMap

    关键点:距离当作 key;距离出现次数当作 value

     1 class Solution 
     2 {
     3     public int numberOfBoomerangs(int[][] points) 
     4     {
     5         HashMap<Integer, Integer> map = new HashMap<>();
     6         int res = 0;
     7         
     8         for(int i=0; i<points.length; i++) // for each point, calculate distance with other points
     9         {
    10             for(int j=0; j<points.length; j++) // iterate other points
    11             {
    12                 if(i == j) // avoid comparing with itself
    13                     continue;
    14                 
    15                 int d = getDistance(points[i], points[j]);
    16                 
    17                 map.put(d, map.getOrDefault(d, 0) + 1);
    18             }
    19             
    20             // iterate values to calculate number of Boomerangs for this point by using 1 * v * (v-1)
    21             for(int value: map.values())
    22                 res += value * (value - 1);  // if value = 1, it will not be count because 1 * 0 = 0
    23                 
    24             
    25             map.clear(); // clear the map
    26         }
    27         
    28         return res;
    29     }
    30     
    31     private int getDistance(int[] a, int[] b)
    32     {
    33         int dx = a[0] - b[0];
    34         int dy = a[1] - b[1];
    35         
    36         return dx * dx + dy * dy;
    37     }
    38 }

    参考资料:

    https://discuss.leetcode.com/topic/66587/clean-java-solution-o-n-2-166ms

    LeetCode 题目列表 - LeetCode Questions List

  • 相关阅读:
    nyoj--76--超级台阶
    nyoj--17--单调递增最长子序列
    poj-2406-Power Strings(KMP)
    poj-1611-The Suspects(并查集)
    poj 2031--Building a Space Station(prim)
    poj 3259-- Wormholes(SPFA)
    nyoj 21--三个水杯(隐式图bfs)
    HDU
    HDU
    CodeForces
  • 原文地址:https://www.cnblogs.com/jimmycheng/p/7807578.html
Copyright © 2020-2023  润新知