作者: 负雪明烛
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. 如何根据fasta快速统计基因组大小及其各染色体长度?

    基因组长度 利用seqkit统计长度 seqkit stat test.fa 结果如下: file format type num_seqs sum_len min_len avg_len max_l ...

  2. SSH客户端工具连接Linux(有的也可以连接Windows、mac、iOS等多系统平台)

    要远程操作Linux的话还是得靠SSH工具,一般来说,Linux是打开了默认22端口的SSH的服务端,如果我们要远程它的话,就需要一个SSH客户. 我对一款好用的工具主要需要满足以下几点. (1)连接 ...

  3. excel-合并多个Excel文件--VBA合并当前目录下所有Excel工作簿中的所有工作表

    在网上找EXCEL多文件合并的方法,思路: 一.Linux 或者window+cmder,直接用命令行cat合并EXCEL文件,但是,需要安装辅助东西才能直接处理(也许也不可以,但是,可以用文件格式转 ...

  4. 给lua_close实现回调函数

    先讲下为什么会需要lua_close回调吧. 我用C++给lua写过不少库,其中有一些,是C++依赖堆内存,并且是每一个lua对象使用一块单独的内存来使用的. 在之前,我一直都是魔改lua源代码,给l ...

  5. 巩固java第五天

    巩固内容: HTML 实例解析 <p> 元素: <p>这是第一个段落.</p> 这个 <p> 元素定义了 HTML 文档中的一个段落. 这个元素拥有一个 ...

  6. cephfs文件系统场景

    创建cephfs文件系统: [cephfsd@ceph-admin ceph]$ cd /etc/ceph [cephfsd@ceph-admin ceph]$ ceph fs ls No files ...

  7. Spark(二十)【SparkSQL将CSV导入Kudu】

    目录 SparkSql 将CSV导入kudu pom 依赖 scala 代码 启动脚本 SparkSql 将CSV导入kudu pom 依赖 <properties> <spark. ...

  8. 零基础学习java------day2------关键字、标志符、常量、进制键的转换、java中的数据类型、强制类型转换的格式

    今日内容要求: 1. 了解关键字的概念及特点,了解保留字 2. 熟练掌握标识符的含义,特点,可使用字符及注意事项 3. 了解常量的概念,进制,进制之间相互转换,了解有符号标识法的运算方式 4. 掌握变 ...

  9. js 如何全部替代一个子串为另一个子串

    更多描述: 假设有一个字符串 `hello. hello. hello. ` 需要替换为 `AAA`,即把 `hello. ` 替换为 `A` 如果需要全量替换字符串,可以使用 String.prot ...

  10. RTTI (Run-time type information) in C++

    In C++, RTTI (Run-time type information) is available only for the classes which have at least one v ...