问题描述:

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

思路:主要学习np.unique()-去重并排序,同时根据return_counts参数可以确定各个元素的个数,squareform()-将点之间距离在简洁和冗余模式相互转化,pdist()-求点之间距离,等函数的应用

代码:

 import numpy as np
from numpy import *
from scipy.spatial.distance import pdist, squareform class Solution:
def numberOfBoomerangs(self, points: List[List[int]]) -> int:
a = squareform(pdist(np.array(points))) result = 0
for i in a:#遍历每一行
count = np.unique(i,return_counts=True)[1]
result += sum(count*(count - 1)) return result

Python3解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. Leetcode: Number of Boomerangs

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

  4. Python3解leetcode Reach a Number

    问题描述: You are standing at position 0 on an infinite number line. There is a goal at position target. ...

  5. Python3解leetcode Single Number

    问题描述: Given a non-empty array of integers, every element appears twice except for one. Find that sin ...

  6. Python3解leetcode N-ary Tree Level Order Traversal

    问题描述: Given an n-ary tree, return the level order traversal of its nodes' values. (ie, from left to ...

  7. Python3解leetcode Maximum Subarray

    问题描述: Given an integer array nums, find the contiguous subarray (containing at least one number) whi ...

  8. Python3解leetcode Count Binary Substrings

    问题描述: Give a string s, count the number of non-empty (contiguous) substrings that have the same numb ...

  9. Python3解leetcode First Bad Version

    问题描述: You are a product manager and currently leading a team to develop a new product. Unfortunately ...

随机推荐

  1. 【转】最全的 pip 使用指南,50% 你可能没用过

    [转]最全的 pip 使用指南,50% 你可能没用过 所有的 Python 开发者都清楚,Python 之所以如此受欢迎,能够在众多高级语言中,脱颖而出,除了语法简单,上手容易之外,更多还要归功于 P ...

  2. 【运维相关】MongoDB那些坑

    前言 某个早期技术债务项目线上有一个mongoDB服务,用途很简单,存一些文件而已.不过用户那边突然报个bug说上边的图片丢失了,起初没当回事认为是代码遇到错了,直到后来看了用户详细的bug复现后,登 ...

  3. 44 答疑(三)--join的写法/Simple nested loop join的性能问题/Distinct和group by的性能/备库自增主键问题

    44 答疑(三) Join的写法 35节介绍了join执行顺序,加了straight_join,两个问题: --1 如果用left join,左边的表一定是驱动表吗 --2 如果两个表的join包含多 ...

  4. sprint test 添加事务回滚机制

    1.原因: 单元测试的时候频繁操作数据库需要修改很多数据,造成不必要的操作,添加事务之后就可以重复对一条数据进行操作,并且在返回结果后进行回滚. 2.解决: 原先继承的是  AbstractJUnit ...

  5. Mac012--FinalShell SSH工具

    Mac--FinalShell SSH工具 FinalShell SSH工具,服务器管理,远程桌面加速软件,支持Windows,Mac OS X,Linux FinalShell,国货产品,同类产品有 ...

  6. 20191003 尚硅谷Spring Cloud教学视频

    视频信息 视频日期:2018-4-19 讲师:尚硅谷周阳 Spring Cloud版本:Dalston.RELEASE 当前版本:Greenwich SR3 微服务.微服务架构.Spring Clou ...

  7. Ant-编译构建(1)-HelloWorld

    1.项目目录构成,lib包暂时为空,本次例子未引入第三方包. 2.编写相关的build.xml <?xml version="1.0" encoding="utf- ...

  8. linux复杂命令

    1,查看包含zypper且不包含ar的进程信息的2,3,8,9列信息 ps -ef|grep zypper|grep -v ar|awk '{print $2,$3,$8,$9}' eg:查看包含zy ...

  9. 【洛谷p1981】表达式求值

    题前废话: 咱也不知道咱写了个什么神奇的代码导致_rqy都看不明白它是怎么re掉的, 代码的大致思路是这样的:对于这样一个中缀表达式,先转化成它的后缀表达式的形式,然后利用P1449 后缀表达式 这道 ...

  10. 洛谷P1095守望者的逃离题解-伪动态规划/贪心

    链接 题目描述 恶魔猎手尤迪安野心勃勃,他背叛了暗夜精灵,率领深藏在海底的娜迦族企图叛变.守望者在与尤迪安的交锋中遭遇了围杀,被困在一个荒芜的大岛上.为了杀死守望者,尤迪安开始对这个荒岛施咒,这座岛很 ...