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 between i 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]]

Solution: Use HashTable, Time: O(N^2), Space: O(N)

我的:注意14行是有value个重复distance,表示有value个点,他们跟指定点距离都是distance,需要选取2个做permutation, 所以是value * (value-1)

 public class Solution {
public int numberOfBoomerangs(int[][] points) {
int res = 0;
for (int i=0; i<points.length; i++) {
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int j=0; j<points.length; j++) {
if (i == j) continue;
int dis = calDistance(points[i], points[j]);
if (!map.containsKey(dis)) map.put(dis, 1);
else map.put(dis, map.get(dis)+1);
}
for (int value : map.values()) {
if (value > 1) {
res += value * (value - 1);
}
}
}
return res;
} public int calDistance(int[] p1, int[] p2) {
int dx = Math.abs(p2[0] - p1[0]);
int dy = Math.abs(p2[1] - p1[1]);
return dx*dx + dy*dy;
}
}

别人的简洁写法

 public int numberOfBoomerangs(int[][] points) {
int res = 0; Map<Integer, Integer> map = new HashMap<>();
for(int i=0; i<points.length; i++) {
for(int j=0; j<points.length; j++) {
if(i == j)
continue; int d = getDistance(points[i], points[j]);
map.put(d, map.getOrDefault(d, 0) + 1);
} for(int val : map.values()) {
res += val * (val-1);
}
map.clear();
} return res;
} private int getDistance(int[] a, int[] b) {
int dx = a[0] - b[0];
int dy = a[1] - b[1]; return dx*dx + dy*dy;
}

Leetcode: Number of Boomerangs的更多相关文章

  1. LeetCode——Number of Boomerangs

    LeetCode--Number of Boomerangs Question Given n points in the plane that are all pairwise distinct, ...

  2. [LeetCode] Number of Boomerangs 回旋镖的数量

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

  3. Python3解leetcode Number of Boomerangs

    问题描述: Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple ...

  4. LeetCode 447. Number of Boomerangs (回力标的数量)

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

  5. 【LeetCode】447. Number of Boomerangs 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 [LeetCode] 题目地址:https:/ ...

  6. [LeetCode]447 Number of Boomerangs

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

  7. 34. leetcode 447. Number of Boomerangs

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

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

  9. C#LeetCode刷题之#447-回旋镖的数量(Number of Boomerangs)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3792 访问. 给定平面上 n 对不同的点,"回旋镖&q ...

随机推荐

  1. 【noiOJ】p8210

    10:河中跳房子 查看 提交 统计 提问 总时间限制:  1000ms 内存限制:  65536kB 描述 每年奶牛们都要举办各种特殊版本的跳房子比赛,包括在河里从一个岩石跳到另一个岩石.这项激动人心 ...

  2. BZOJ4154: [Ipsc2015]Generating Synergy

    Description 给定一棵以1为根的有根树,初始所有节点颜色为1,每次将距离节点a不超过l的a的子节点染成c,或询问点a的颜色   Input 第一行一个数T,表示数据组数 接下来每组数据的第一 ...

  3. Maven with Multi-module

    Well, A project made of multi modules. For example, the hongten-security project, the structure of t ...

  4. CF #374 (Div. 2) C. Journey dp

    1.CF #374 (Div. 2)    C.  Journey 2.总结:好题,这一道题,WA,MLE,TLE,RE,各种姿势都来了一遍.. 3.题意:有向无环图,找出第1个点到第n个点的一条路径 ...

  5. 一、午夜倒数《苹果iOS实例编程入门教程》

    该app为应用的功能为计算离午夜12:00点的剩余时间 现版本 SDK 8.4 Xcode 运行Xcode 选择 Create a new Xcode project ->Single View ...

  6. #nav li:hover ul 与#nav li a:hover ul 的区别

    #nav li:hover ul 与#nav li a:hover ul 有什么区别? ──────────────────────────────────────────── #nav li:hov ...

  7. JAVA 往jar包添加class文件

    (1) jar -uf jarfile.jar yourclasses (2) 右击要打包的文件夹,选择“添加到压缩文件”,弹出对话框: 把压缩文件格式改为zip,再把压缩文件名中的反缀改为.jar, ...

  8. JS 心得总结

    1.浏览器关闭事件监听(http://pengjianbo1.iteye.com/blog/507569,http://bbs.csdn.net/topics/360152711) <!DOCT ...

  9. .net 文件下载【转】

    方式一:TransmitFile实现下载.将指定的文件直接写入 HTTP 响应输出流,而不在内存中缓冲该文件.     protected void Button1_Click(object send ...

  10. cloudsim安装,配置(到eclipse)

    现在基本成功了.所以将这个过程尽量详细的,准确的分享出来,以供大家的需要.       一.Jdk,Eclipse的安装与配置. 本人下载的jdk版本是1.8,jdk的相关配置网上有很多,我就不赘述了 ...