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, ...
随机推荐
- js脚本根据身份证号获取性别、年龄、家庭地址、生日
做项目测试时需要根据身份证号获取其信息,也不想调接口,就自己在本地通过收集资料整合了一个
- hdu 1150 Machine Schedule 最小覆盖点集
题意:x,y两台机器各在一边,分别有模式x0 x1 x2 ... xn, y0 y1 y2 ... ym, 现在对给定K个任务,每个任务可以用xi模式或者yj模式完成,同时变换一次模式需要重新启动一次 ...
- PHP进程锁
<?php /** * CacheLock 进程锁,主要用来进行cache失效时的单进程cache获取,防止过多的SQL请求穿透到数据库 * 用于解决PHP在并发时候的锁控制,通过文件/eacc ...
- angularJS简单调用接口,实现数组页面打印
相比较jquery ,angular对这种接口数据处理起来会方便的多.这里举例调用 中国天气网的api接口. 首先肯定要引入angular.js这个不多说 <link rel="sty ...
- PhpStorm2017版激活方法、汉化方法以及界面配置
PhpStorm激活和汉化文件下载网址:http://pan.baidu.com/s/1nuHF1St(提取密码:62cg) PHPMailer的介绍 PhpStorm是一个轻量级且便捷的PHP ID ...
- Git快速入门和常用命令
一.快速入门 本地初始化一个项目 首先,你需要执行下面两条命令,作为 git 的基础配置,作用是告诉 git 你是谁,你输入的信息将出现在你创建的提交中. git config --global us ...
- win10下Python3.6安装、配置以及pip安装包教程
0.目录 1.前言 2.安装python 3.使用pip下载.安装包 3.1 安装Scrapy 3.2 安装PyQt 3.3 同时安装多个包 3.4 pip的常用命令 1.前言 之前在电脑上安装了py ...
- 网页设计——5.table布局
今天做一个大的页面,主要是对table布局的理解: 代码: <table cellspacing=0 border=1 style="bordercolor:#C0C0C0;" ...
- 41.Linux应用调试-修改内核来打印用户态的oops
1.在之前第36章里,我们学习了通过驱动的oops定位错误代码行 第36章的oops代码如下所示: Unable to handle kernel paging request at //无法处理内核 ...
- day10、nfs+rsync全网备份及实时同步
题目要求 注意:博主使用的系统为: [root@web01 ~]# uname -a Linux web01 2.6.32-696.el6.x86_64 #1 SMP Tue Mar 21 19:29 ...