leetcode_447. Number of Boomerangs 平面上距离相等的点的组数
来源:互联网 发布:温十如何解除网络禁用 编辑:程序博客网 时间:2024/06/06 10:42
题目:
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:2Explanation:The two boomerangs are [[1,0],[0,0],[2,0]] and [[1,0],[2,0],[0,0]]
题意:
二维平面上给定n个点,找出一些点对(i,j,k),其中点i到点j与点i到点k的距离相等,返回点对的数量
代码:
class Solution(object):
def numberOfBoomerangs(self, points):
"""
:type points: List[List[int]]
:rtype: int
"""
n = len(points)
if n < 3 :
return 0
else :
res = 0 #记录boomerangs数量
for i in range(n) : #第一层循环,遍历所有点
dis = dict() #用字典记录点i到其他点的距离,key为距离,value为这个距离对应的点的个数
for j in range(n) : #第二层循环,计算点i到点j的距离
if j != i :
temp = (points[i][0]-points[j][0])**2 + (points[i][1]-points[j][1])**2 #计算距离,保持距离为平方和的形式
if temp not in dis :
dis[temp] = 0
dis[temp] += 1 #该距离对应的点的个数加1
for k in dis :
if dis[k] >= 2 :
res += dis[k]*(dis[k]-1) #如果某个距离对应的点数量count大于2,计算这些点构成的点对数量为count*(count-1)
return res
笔记:
假设已知点i对应的距离为dis的点的个数为n,则一共有 2*Cn2 = 2*(n*(n-1)/2) = n*(n-1) 种排列组合,故可能存在的点对需要在原来基础上加 n*(n-1)
- leetcode_447. Number of Boomerangs 平面上距离相等的点的组数
- 447. Number of Boomerangs | 点之间的距离相等的数量
- 平面中用到的公式(点到平面的距离、平面上的最近点)
- 447. Number of Boomerangs的C++解法
- 点到平面的距离
- 点到平面的距离
- 点到平面的距离
- 点到平面的距离
- 点到平面的距离
- 平面上点对的最小距离算法-java实现
- 平面上最近点对的距离 POJ 3714
- 找出平面上距离最近的两个点
- 分治,在平面上找距离最近的两个点
- 平面上的点
- Number of Boomerangs(统计"boomerang"的数目)
- leetcode_447("boomerang"的个数)
- 给定平面上的N个点,寻找距离最远的两个点
- 给定平面上的N各点,寻找距离最远的两个点
- this运算符
- 无刷电机控制器
- D3.js symbol 符号生成器
- 结构体初始化
- 6款免费的PDF解锁软件
- leetcode_447. Number of Boomerangs 平面上距离相等的点的组数
- 在Ubuntu环境下搭建Cross Compiler后如何编译hostapd
- js深拷贝和浅拷贝
- 汇编语言---GCC内联汇编
- ios UIView的回调方法
- ContentProvider简介
- 动态库调用静态库示例讲解
- Kali Linux 网络扫描秘籍 第七章 Web 应用扫描(二)
- DAY24:leetcode #66 Plus One