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学习笔记 变量

    蒟蒻高举横幅:部分内容转自廖雪峰的Python教学 1.Python是动态语言,即它的变量是没有类型的. !/usr/bin/env python a = 'ABC' print a a = 123 ...

  2. SpringMVC中遇到页面跳转出现404错误的问题

    今天遇到了一个问题: 使用SpringMVC时,出现页面无法跳转的情况(404错误), 出现这个异常的原因在于SpringMVC的配置文件中控制器的配置书写错误: 原代码: <context:c ...

  3. Heap Sorting 总结 (C++)

    各位读者,大家好. 因为算法和数据结构相关的知识都是在国外学的,所以有些词汇翻译的可能不准确,然后一些源代码的注释可能是英文的,如有给大家带来什么不方便,请见谅.今天我想写一下Heap相关的知识,从基 ...

  4. HTML的iframe标签妙用 - 在线执行前端代码的网站原理是什么?

    在我自己的日常前端开发中,其实iframe标签出现的次数并不是很多.作为一个很古老(浏览器兼容性非常好)的标签,有必要去了解一下它的典型应用场景. (图片说明:所有浏览器都支持iframe,无论什么版 ...

  5. expressjs 支持umeditor 的图片上传

    umedtor 的文件上传 是form 提交. 请求的header Accept: text/html,application/xhtml+xml,application/xml;q=0.9,imag ...

  6. 实践作业2:黑盒测试实践——安装配置测试工具 Day 3

    安装配置Katalon Studio工具 1.找下载Katalon链接,需要有效电子邮件才能激活Katalon账户,在我的Gmail里注册. 2.选择下载版本 3.下载完成后 ,转到计算机上的文件夹, ...

  7. CI框架使用PHPmailer发送邮件找回密码

    之前用PHP+Mysql+jQuery结合ThinkPHP做了一个用户验证邮箱找回密码功能<ThinkPHP之PHP+Mysql+jQuery发送邮箱找回密码>,现在分享一下用CI框架结合 ...

  8. C# 通过反射初探ORM框架的实现原理

    背景: 以前学的Java进行开发,多用到Mybatis,Hiberante等ORM框架,最近需要上手一个C#的项目,由于不是特别难,也不想再去学习C#的ORM框架,所以就想着用反射简单的实现一下ORM ...

  9. PHPstorm 函数时间注释的修改

    正常的PHPstorm里面函数方法的注释是没有动态时间设置的,但是看了PHP file里面有时间日期的注释,而PHP Function Doc Comment 却没有,让很多PHPer很头疼,今天在搜 ...

  10. 非常棒的教程记录(UML)

    这里暂且记录下看过的非常棒的博客吧! 来自 CSDN 几年前的博客专栏了,我只想说:经典实用的知识永远不会过时! http://blog.csdn.net/column/details/umlmode ...