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

 class Solution
{
public int numberOfBoomerangs(int[][] points)
{
HashMap<Integer, Integer> map = new HashMap<>();
int res = 0; for(int i=0; i<points.length; i++) // for each point, calculate distance with other points
{
for(int j=0; j<points.length; j++) // iterate other points
{
if(i == j) // avoid comparing with itself
continue; int d = getDistance(points[i], points[j]); map.put(d, map.getOrDefault(d, 0) + 1);
} // iterate values to calculate number of Boomerangs for this point by using 1 * v * (v-1)
for(int value: map.values())
res += value * (value - 1); // if value = 1, it will not be count because 1 * 0 = 0 map.clear(); // clear the map
} return res;
} private int getDistance(int[] a, int[] b)
{
int dx = a[0] - b[0];
int dy = a[1] - b[1]; return dx * dx + dy * dy;
}
}

参考资料:

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

LeetCode 题目列表 - LeetCode Questions List

LeetCode 447. Number of Boomerangs (回力标的数量)的更多相关文章

  1. 447. Number of Boomerangs 回力镖数组的数量

    [抄题]: Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple ...

  2. [LeetCode]447 Number of Boomerangs

    Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of po ...

  3. 34. leetcode 447. Number of Boomerangs

    Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of po ...

  4. 447 Number of Boomerangs 回旋镖的数量

    给定平面上 n 对不同的点,“回旋镖” 是由点表示的元组 (i, j, k) ,其中 i 和 j 之间的距离和 i 和 k 之间的距离相等(需要考虑元组的顺序).找到所有回旋镖的数量.你可以假设 n ...

  5. [LeetCode] Number of Boomerangs 回旋镖的数量

    Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of po ...

  6. 【LeetCode】447. Number of Boomerangs 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 [LeetCode] 题目地址:https:/ ...

  7. [LeetCode&Python] Problem 447. Number of Boomerangs

    Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of po ...

  8. [LeetCode] 200. Number of Islands 岛屿的数量

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  9. 447. Number of Boomerangs

    Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of po ...

随机推荐

  1. python学习笔记(5)—— tuple 本质探究

    >>> t=(1,2,3,['a','b','c'],4,5) >>> t[3][0]='x' >>> t (1, 2, 3, ['x', 'b' ...

  2. 远程图形界面:VncServer与KDE桌面远程连接

           强烈不建议putty+xming的工作方式,太慢了,且界面也不是特别友好.        原文:vncserver与KDE远程连接        建议:不过还是建议使用Xshell,使用 ...

  3. HDU_1556_线段树区间更新

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  4. sql IIF函数的应用

    工作中由于用各种框架,业务也不是很复杂,一直没怎么写过复杂的sql,今天写了一个 数据如下 代码如下 Sub 班级分数等级分析(shname) Dim Con As Object, rs As Obj ...

  5. CAD得到所有实体2

    主要用到函数说明: IMxDrawSelectionSet::Select2 构造选择集.详细说明如下: 参数 说明 [in] MCAD_McSelect Mode 构造选择集方式 [in] VARI ...

  6. ThinkPHP---框架介绍

    (1)什么是框架? ①框架是一堆包含了常量.方法和类等代码集合: ②半成品应用,只包含了项目开发时的底层架构,并不包含业务逻辑: ③包含一些设计模式,例如单例模式,工厂模式,AR(Active Rec ...

  7. ocelot+consul+identity4的使用demo

    Ocelot网关搭建 搭建core控制台项目 本demo采用2.1版本 命名为APPIGateWay 在Nuget包中添加Ocelot引用 选用8.0.0版本 添加Ocelot.json 文件 内容为 ...

  8. spark学习(1)---dataframe操作大全

    一.dataframe操作大全 https://blog.csdn.net/dabokele/article/details/52802150 https://www.jianshu.com/p/00 ...

  9. Plan & Future

    以下是OI省选前的数据结构与算法整理,可能还不是很全面.但是已经是全网相对比较全面的了.所有标记为“基础”“进阶”“中级”“提高”的知识为近些年来NOIp考察的内容,需重点掌握. 所有“高级”部分为N ...

  10. python3 http.server备忘

    python3英文的 打印出来应该不错: https://docs.python.org/3/library/http.server.html#module-http.server python2.7 ...