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 betweeni and 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]]

在一个数组中找到满足以下条件的三元组(i,j,k)的个数

  • 1.要求(i,j)的欧氏距离等于(i,k)

  • 2.(i,k,j)(j,k,i)为不同


public int distance(int a[],int b[])
{
int x = a[0] - b[0];
int y = a[1] - b[1];
return x*x + y*y;
}
public int numberOfBoomerangs(int[][] points) {//[a,b,c,d]
int result = 0;
HashMap<Integer,Integer> map = new HashMap<>();
for(int i = 0; i < points.length; i++)
{
for(int j = 0; j < points.length ; j++) //内层循环,计算ab,ac,ad距离
{
if(i != j)
{
int dis = distance(points[i], points[j]);
map.put(dis, map.getOrDefault(dis, 0) + 1); //类似python a.get(key,default)
}
}
for(int val:map.values())
result += val * (val-1); //全排列 A(n,2)
map.clear(); //清空map
} return result;
}

在一次循环中,遍历所有的点,找出任意两个点的距离,例如(ab,ac,ad),并保存距离为d个组合个数n。由于条件2,相当于全排列A(n,2)

知识点:

  • 1.map获取值的方式map.getOrDefault(Object key, Integer defaultValue)类似与python的dict的get

  • 2.map.values的用法,类似的有map.keySet()

447. Number of Boomerangs的更多相关文章

  1. [LeetCode]447 Number of Boomerangs

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

  2. 34. leetcode 447. Number of Boomerangs

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

  3. [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 ...

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

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

  5. LeetCode 447. 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. 447 Number of Boomerangs 回旋镖的数量

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

  8. 【leetcode】447. Number of Boomerangs

    题目如下: 解题思路:我首先用来时间复杂度是O(n^3)的解法,会判定为超时:后来尝试O(n^2)的解法,可以被AC.对于任意一个点,我们都可以计算出它与其余点的距离,使用一个字典保存每个距离的点的数 ...

  9. [刷题] 447 Number of Boomerangs

    要求 给出平面上n个点,寻找存在多少点构成的三元组(i j k),使得 i j 两点的距离等于 i k 两点的距离 n 最多为500,所有点坐标范围在[-10000, 10000]之间 示例 [[0, ...

随机推荐

  1. Kafka设计解析(八)- Exactly Once语义与事务机制原理

    原创文章,首发自作者个人博客,转载请务必将下面这段话置于文章开头处. 本文转发自技术世界,原文链接 http://www.jasongj.com/kafka/transaction/ 写在前面的话 本 ...

  2. centos6.5 短信猫部署发短信

    本文为在centos下部署短信猫发短信使用,以下为具体环境和步骤说明,欢迎留言! 一.环境说明 服务器:centos6.5 x64 依赖包:lockdev-1.0.1-18.el6.x86_64.rp ...

  3. websocket介绍

    websocket应用 手动实现的websocket 你所见过的websocket 你一定见过在网站中,有一个游客聊天的聊天框,比如人人影视.这个聊天框是如何实现即时通讯的呢,就是用到了websock ...

  4. springmvc中对日期格式化的处理

    @DateTimeFormat(pattern="yyyy-MM-dd") 返回的时候java.util.Date pattern="yyyy-MM-dd"必须 ...

  5. 垃圾陷阱洛谷dp

    题目描述 卡门――农夫约翰极其珍视的一条Holsteins奶牛――已经落了到“垃圾井”中.“垃圾井”是农夫们扔垃圾的地方,它的深度为D(2<=D<=100)英尺. 卡门想把垃圾堆起来,等到 ...

  6. 51Nod 2006 飞行员配对(二分图最大匹配)

    链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=2006 思路: 二分匹配 注意n m的关系 代码: #include ...

  7. HDU1171--01背包

    Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  8. 前端测试框架Jest系列教程 -- Global Functions(全局函数)

    写在前面: Jest中定义了很多全局性的Function供我们使用,我们不必再去引用别的包来去实现类似的功能,下面将列举Jest中实现的全局函数. Jest Global Functions afte ...

  9. Windows中根据端口定位持有者程序

    之前被一个问题所困扰, 电脑过一段时间就无法上网, 查询netstat, 会有大量的端口被占用, 但是看不出来是哪个程序开启的 ( 最终定位到是ADSafe搞得鬼 ) . 后来发现几个命令可以根据端口 ...

  10. Java 面试宝典

    1. 类的实例化顺序,比如父类静态数据,构造函数,字段,子类静态数据,构造函数,字段,他们的执行顺序 答:先静态.先父后子. 先静态:父静态 > 子静态 优先级:父类 > 子类 静态代码块 ...