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 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的get2.map.values的用法,类似的有map.keySet()
447. Number of Boomerangs的更多相关文章
- [LeetCode]447 Number of Boomerangs
Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of po ...
- 34. leetcode 447. Number of Boomerangs
Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of po ...
- [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 ...
- 447. Number of Boomerangs 回力镖数组的数量
[抄题]: Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple ...
- LeetCode 447. Number of Boomerangs (回力标的数量)
Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of po ...
- 【LeetCode】447. Number of Boomerangs 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 [LeetCode] 题目地址:https:/ ...
- 447 Number of Boomerangs 回旋镖的数量
给定平面上 n 对不同的点,“回旋镖” 是由点表示的元组 (i, j, k) ,其中 i 和 j 之间的距离和 i 和 k 之间的距离相等(需要考虑元组的顺序).找到所有回旋镖的数量.你可以假设 n ...
- 【leetcode】447. Number of Boomerangs
题目如下: 解题思路:我首先用来时间复杂度是O(n^3)的解法,会判定为超时:后来尝试O(n^2)的解法,可以被AC.对于任意一个点,我们都可以计算出它与其余点的距离,使用一个字典保存每个距离的点的数 ...
- [刷题] 447 Number of Boomerangs
要求 给出平面上n个点,寻找存在多少点构成的三元组(i j k),使得 i j 两点的距离等于 i k 两点的距离 n 最多为500,所有点坐标范围在[-10000, 10000]之间 示例 [[0, ...
随机推荐
- 程序员的自我救赎---1.4.2: 核心框架讲解(BLL&Tool)
<前言> <目录> (一) Winner2.0 框架基础分析 (二) 短信中心 (三)SSO单点登录 (四)PLSQL报表系统 (五)钱包系统 (六)GPU支付中心 (七)权限 ...
- 集合、set以及HASH
集合的数据结构数据结构就是内存中保存输出数据的形式,不同的数据结构会有不同的特征.堆栈结构:先进后出 代表类(stack):应用场景:java中的方法运行时所占用的空间就是这种结构.队列结构:先进先出 ...
- Ipython自动导入Numpy,pandas等模块
一.引言 最近在学习numpy,书上要求安装一个Ipythpn,可以自动导入Numpy,pandas等数据分析的模块,可是当我安装后,并不能自动导入numpy模块,还需要自己import.我就去查了一 ...
- Linux运维项目实战系列
Linux运维项目实战系列 项目实战1-LNMP的搭建.nginx的ssl加密.权限控制的实现 项目实战2-项目实战2-实现基于LVS负载均衡集群的电商网站架构 2.1项目实战2.1-nginx 反向 ...
- Mysql 的 IF 判断
mysql自带很多判断逻辑,今天说一说IF的判断语句,正好今天做项目的时候也用到了 1. IF 判断 IF判断和我们代码里面写的有略微的差别,举个例子 IF('表达式','结果1','结果2') 如 ...
- post 与get 区别
刷新/后退按钮 GET后退按钮/刷新无害,POST数据会被重新提交(浏览器应该告知用户数据会被重新提交). 书签 GET书签可收藏,POST为书签不可收藏. 缓存 GET能被缓存 缓存是针对URL来进 ...
- java web学习笔记 servlet
关于java web web.xml中一般配置的都是与servlet先关的可以配置servlet filter listener context-param用来配置web应用的启动参数,可用通过Ser ...
- 在websphere上部署集群应用程序-工作记录
1) 创建web集群.client集群,添加集群托管节点,根据需求来,我的需求是两个应用部署到4个服务器上,属于1主3备模式 2) 创建webspere变量:选择你需要的集群作用域,新建资源 (作 ...
- android boot.img
android在启动时uboot推断有没有组合健按下或者cache分区的升级文件来决定进入哪个系统(可能还有别的推断方式) 有组合健按下或者cache分区有升级文件,则载入recovery.img进入 ...
- codevs1050
题目地址:http://codevs.cn/problem/1050/ 分析: 最開始想直接用状压做,发现怎么都想不出来.就和当年的多行多米诺骨牌(这道题至少最后还是把普通状压做法看懂了). 直到听到 ...