LeetCode 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 between iand 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]]
题目标签: Hash Table
题目给了我们一个 点坐标的 array,让我们找到“回力标”的数量。
首先我们看题目中的例子:
0,0 1,0 2,0 这里 1,0 到 0,0 的距离 等于 到 2,0 的距离;
根据题意,它有2种可能性 1,0 0,0 2,0 和 1,0 2,0 0,0
那么如果有一个点,它到其他三个点的距离都一样,那么对于这个点,它有几种可能性呢?
它的组合是 三个点: p1 p2 p3
首先p1 都是它自己,不会变;
p2 会从3个点中选一个;
p3 会从选剩下的2个点中选一个;
所以答案就是: 1 * 3 * 2 公式就是 1 * n * (n-1)
搞清楚这点以后,我们可以遍历每一个点:
对于每一个点,把它与其他所有点之间的距离,计算之后当作 key 存入 map,它的出现次数当作 value 存入;
如果一个距离的 value = 3, 说明 这个点 到3 个点的距离是一样的,所以只要遍历 value, 把所有的value * (value - 1) 进行累加。
要注意的是,如果value = 1,说明点a 到点b = key,并没有点a 到 点c 这个c 存在,不需要加入累加,我们也不需要做什么,因为 1 * 0 = 0。
还有一点就是 两点之间的距离公式,这里并没有开根号,因为并不影响结果,这样更简单方便。
Java Solution:
Runtime beats 79.05%
完成日期:06/06/2017
关键词:HashMap
关键点:距离当作 key;距离出现次数当作 value
class Solution
{
public int numberOfBoomerangs(int[][] points)
{
HashMap<Integer, Integer> map = new HashMap<>();
int res = 0; for(int i=0; i<points.length; i++) // for each point, calculate distance with other points
{
for(int j=0; j<points.length; j++) // iterate other points
{
if(i == j) // avoid comparing with itself
continue; int d = getDistance(points[i], points[j]); map.put(d, map.getOrDefault(d, 0) + 1);
} // iterate values to calculate number of Boomerangs for this point by using 1 * v * (v-1)
for(int value: map.values())
res += value * (value - 1); // if value = 1, it will not be count because 1 * 0 = 0 map.clear(); // clear the map
} 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;
}
}
参考资料:
https://discuss.leetcode.com/topic/66587/clean-java-solution-o-n-2-166ms
LeetCode 题目列表 - LeetCode Questions List
LeetCode 447. Number of Boomerangs (回力标的数量)的更多相关文章
- 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 ...
- 34. leetcode 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 回旋镖的数量
给定平面上 n 对不同的点,“回旋镖” 是由点表示的元组 (i, j, k) ,其中 i 和 j 之间的距离和 i 和 k 之间的距离相等(需要考虑元组的顺序).找到所有回旋镖的数量.你可以假设 n ...
- [LeetCode] 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:/ ...
- [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 ...
- [LeetCode] 200. Number of Islands 岛屿的数量
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
- 447. Number of Boomerangs
Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of po ...
随机推荐
- 项目经验——Sql server 数据库的备份和还原____还原数据库提示“介质集有2个介质簇,但只提供了1个。必须提供所有成员” .
在对数据库备份与还原的过程中,我遇到一个问题“介质集有2个介质簇,但只提供了1个.必须提供所有成员”,下面详细的介绍一下遇到问题的经过与问题解决的方法! 一.备份与还原遇到的问题描述与解决方法: 前两 ...
- 关于c++11中static类对象构造函数线程安全的验证
在c++11中,static静态类对象在执行构造函数进行初始化的过程是线程安全的,有了这个特征,我们可以自己动手轻松的实现单例类,关于如何实现线程安全的单例类,请查看c++:自己动手实现线程安全的c+ ...
- 数据库–Cobar分布式数据库集群MySQL中间件
运行环境: 主机1:Ubuntu14.04 Desktop + MySQL5.5 + JDK 1.7(HP Z400) 内网IP地址:192.168.137.8 NODE1:Ubuntu 13.04 ...
- 给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null。
package algorithms; /* 给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null. public class ListNode { int val; ListNo ...
- Redis系列(七)--Sentinel哨兵模式
在上一篇文章了解了主从复制,主从复制本身的容错性很差,一旦master挂掉,只能进行手动故障转移,很难完美的解决这个问题 而本文讲解的sentinel可以解决这个问题 Redis sentinel示意 ...
- enote笔记语言(2)(ver0.5)
why not(whyn't) 为什么不(与“why”相反对应,是它的反面.它的矛盾对立面) how对策 how设计 key-memo: ...
- Python使用Flask框架,结合Highchart,搭配数据功能模块,加载 HTML 表格数据
参考链接:https://www.highcharts.com.cn/docs/data-modules 1.javascript代码 var chart = Highcharts.chart('co ...
- [Python3网络爬虫开发实战] 1.1-Python3的安装
既然要用Python 3开发爬虫,那么第一步一定是安装Python 3.这里会介绍Windows.Linux和Mac三大平台下的安装过程. 1. 相关链接 官方网站:http://python.org ...
- PHP 计数排序
计数排序不是基于比较的排序算法,其核心在于将输入的数据值转化为键存储在额外开辟的数组空间中. 作为一种线性时间复杂度的排序,计数排序要求输入的数据必须是有确定范围的整数. 算法描述 找出待排序的数组中 ...
- 洛谷 4216 BZOJ 4448 [SCOI2015]情报传递
[题解] 每个情报员的危险值val[i]应该是一个分段函数,前面一段是平行于x轴的横线,后面一段是一次函数.我们可以用fx(t)=t-b[x]表示这个一次函数.每次询问一条链上fx(t)大于c的点的个 ...