作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


[LeetCode]

题目地址:https://leetcode.com/problems/number-of-boomerangs/

  • Difficulty: Easy

题目描述

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

题目大意

如果一个三元组,第一个元素和第二个元素的距离 等于 第一个元素和第三个元素的距离,那么是一个回旋镖。问题目给出的n个长度的点中,有多少个回旋镖。

解题方法

题目的意思是找出距离到其他的点都相等的点,并且判断相等的路径的排列有多少条。

尝试暴力解决。

双重循环是没有问题的,因为题目要求出一个点到其余各个点的距离,因此,必须有双重循环。在存储距离的时候,要使用HashMap,这里有个技巧,可以节省代码:hashmap.put(distance, hashmap.getOrDefault(distance, 0) + 1);,如果没有这个距离的话,就放入1,有的话就把之前的值加1。

在这个大的循环里边,遍历完到其余个点的距离后,再把HashMap中的距离相等的值给拿出来,如果某个点到其他的点的距离都不相等,那么这个值是1,所以结果为0;否则是N*(N-1).

public class Solution {
public int numberOfBoomerangs(int[][] points) {
int res = 0;
HashMap<Integer, Integer> hashmap=new HashMap<>();
for(int i =0; i< points.length; i++){
for(int j=0; j< points.length; j++){
if(i == j){
continue;
}
int distance = getDistance(points[i], points[j]);
hashmap.put(distance, hashmap.getOrDefault(distance, 0) + 1); }
for(int val : hashmap.values()) {
res += val * (val-1);
}
hashmap.clear();
}
return res;
}
public int getDistance(int[] point1, int[] point2){
int dx= point2[0] - point1[0];
int dy=point2[1] - point1[1];
return dx*dx + dy*dy;
}
}

AC: 188 ms 超过61%

python写法:

class Solution:
def numberOfBoomerangs(self, points):
"""
:type points: List[List[int]]
:rtype: int
"""
res = 0
for p0 in points:
d = collections.defaultdict(int)
for p1 in points:
d[(p0[0] - p1[0]) ** 2 + (p0[1] - p1[1]) ** 2] += 1
for d, v in d.items():
res += v * (v - 1)
return res

日期

2017 年 1 月 12 日
2018 年 11 月 14 日 —— 很严重的雾霾

【LeetCode】447. Number of Boomerangs 解题报告(Java & Python)的更多相关文章

  1. 【LeetCode】575. Distribute Candies 解题报告(Java & Python)

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

  2. 【LeetCode】383. Ransom Note 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...

  3. 【LeetCode】283. Move Zeroes 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:首尾指针 方法二:头部双指针+双循环 方法三 ...

  4. 【LeetCode】376. Wiggle Subsequence 解题报告(Python)

    [LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...

  5. 【LeetCode】911. Online Election 解题报告(Python)

    [LeetCode]911. Online Election 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...

  6. 【LeetCode】870. Advantage Shuffle 解题报告(Python)

    [LeetCode]870. Advantage Shuffle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn ...

  7. 【LeetCode】435. Non-overlapping Intervals 解题报告(Python)

    [LeetCode]435. Non-overlapping Intervals 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemi ...

  8. 【LeetCode】498. Diagonal Traverse 解题报告(Python)

    [LeetCode]498. Diagonal Traverse 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: htt ...

  9. 【LeetCode】481. Magical String 解题报告(Python)

    [LeetCode]481. Magical String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:/ ...

随机推荐

  1. 【Proteogenomis】工具评述

    2015年的综述文章,marker一下,供以后比较试用.蛋白质基因组学研究中的质谱仪与生物信息学方法. 针对蛋白质基因组学的分析研究,总结了质谱仪和蛋白质基因组学流程分析软件的选择,重点评述了常用的生 ...

  2. 通过mac地址确认二层交换机某个端口下接的终端设备IP

    正常来说,二层交换机主要是通过mac地址进行通信的,这就导致我们无法直接通过arp表来确认交换机端口下终端设备的IP: 但我们仍然能通过查找二层交换机端口下学习到的mac地址,然后通过对照三层核心交换 ...

  3. 45-Letter Combinations of a Phone Number

    Letter Combinations of a Phone Number My Submissions QuestionEditorial Solution Total Accepted: 7855 ...

  4. C++你不知道的事

    class A { public: A() { cout<<"A's constructor"<<endl; } virtual ~A() { cout&l ...

  5. 码上来战!探索“智”感生活,HMS Core线上Codelabs挑战赛第4期开始!

    HMS Core线上Codelabs挑战赛第4期正式开始!我们向所有实践力超强.创新力满满的开发者发出邀请,用你的超级"码"力,解锁更多应用价值! 生活里,我们被手机"秒 ...

  6. MPI 学习笔记

    目录 MPI学习笔记 MPI准备 概述 前置知识补充 环境部署 1.修改IP及主机名 2.关闭防火墙 3.实现免密码SSH登录 4.配置MPI运行环境 5.测试 程序的执行 编译语句 运行语句 MPI ...

  7. 关于java中的安全管理器

    最近再查看java的源码的时候看见了这一类代码 final SecurityManager sm = System.getSecurityManager(); 想要了解这个是为了做什么,查看资料之后发 ...

  8. LINUX 安装增强 前置安装文件

    yum install kernel yum install kernel-devel yum install gcc yum install make

  9. What happens when more restrictive access is given to a derived class method in C++?

    We have discussed a similar topic in Java here. Unlike Java, C++ allows to give more restrictive acc ...

  10. layui 弹窗中 分页展示table

    1. 需求:点击查看更多,展示该类别 所有数据,并分页 2. 参考文档: (1)https://www.jianshu.com/p/40da11ebae66 (2) https://blog.csdn ...