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. 一个可扩展的深度学习框架的Python实现(仿keras接口)

    一个可扩展的深度学习框架的Python实现(仿keras接口) 动机 keras是一种非常优秀的深度学习框架,其具有较好的易用性,可扩展性.keras的接口设计非常优雅,使用起来非常方便.在这里,我将 ...

  2. java二进制相关基础

    转载请注明原创出处,谢谢! 说在前面 之前在JVM菜鸟进阶高手之路十(基础知识开场白)的时候简单提到了二进制相关问题,最近在看RocketMQ的源码的时候,发现涉及二进制的内容蛮多,jdk源码里面也是 ...

  3. Ipad弹出UIAlertControllerStyleActionSheet时发生崩溃

    pad弹出UIAlertControllerStyleActionSheet时,在iphone上运行正常,但在ipad上崩溃,解决代码如下: UIAlertController *alertVc = ...

  4. Linux系统bash shell之历史命令

    1.相关变量: HISTSIZE: 定义命令历史记录的条数 HISTFILE: 定义命令储存的文件,一般是 ~/.bash_history HISTFILESIZE: 定义了历史文件记录历史的条数 H ...

  5. python并发编程之多线程一

    一,什么是线程 线程也被称为轻量进程计算机科学术语,指运行中的程序的调度单位. 线程是进程中的实体,一个进程可以拥有多个线程,一个线程必须有一个父进程.线程不拥有系统资源,只有运行必须的一些数据结构: ...

  6. JavaScript OOP(一)之构造函数与new命令

    面向对象编程:Object Oriented Programming,简称OOP. 典型的oop语言,如hava.c++,存在着类的概念,类就是对象的模板 (类可以类比为人类:而实例化类后变为对象,对 ...

  7. yii2.0中添加二维数组,多条数据。

    /** * @inheritdoc 批量添加 * @params $add array 添加数据 */public function add_all($add){ $connection = \Yii ...

  8. 移动GIS未来的前景GIS解决方案应用

    目前,在我国一些发达地区,移动GIS所需的两个技术前提——移动互联网的普及和智能终端的普及都已经得到了满足,特别是平板电脑.智能手机的流行,苹果产品的风靡,为移动GIS的应用提供了肥沃的土壤,而GIS ...

  9. Cs Round#54 D Spanning Trees

    题意:构造一张N个结点无重边.无自环的无向图.使得其最小生成树和最大生成树共享K条边. 样例一很具有启发性: 当K!=0时,我们可以先构造出一条链,链的长度为n-k的链,作为最小生成树的一部分,之后由 ...

  10. Java数据结构和算法(十)——二叉树

    接下来我们将会介绍另外一种数据结构——树.二叉树是树这种数据结构的一员,后面我们还会介绍红黑树,2-3-4树等数据结构.那么为什么要使用树?它有什么优点? 前面我们介绍数组的数据结构,我们知道对于有序 ...