问题描述:

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. 阶段1 语言基础+高级_1-3-Java语言高级_03-常用API第二部分_第2节 Date类_5_练习_计算出一个人已经出生了多少天

    Alt+回车

  2. dockerFile 配置puppeteer

    ## install npm && puppeteer## 必要依赖 libXScrnSaver RUN yum -y install libXScrnSaver ## install ...

  3. struts2 基础2 类型转换器

    struts2常用常量的定义与意义 每一次请求都会创建一个新的action,所以struts2的action是线程安全的 拆分struts 为应用指定多个struts配置文件 src 下为各应用配置的 ...

  4. ementUi rules表单验证 --》Wangqi

    ElementUi rules表单验证   ElementUi 表单验证 工作中常用到的JS验证 可以在pattern中书写正则,并且配合elementUI进行表单验证. pattern 属性规定用于 ...

  5. Java WebService 参考文档

    webservice 基础使用 java 与tomcat使用http://cxshun.iteye.com/blog/1275408 spring mvc中使用https://www.cnblogs. ...

  6. CentOS7 iptables安装及操作

    添加规则时的考量点: (1)要实现哪种功能:判断添加在哪张表上: (2)报文流经的路径:判断添加在哪个链上: 链上规则的次序: (1)同类规则(访问同一应用),匹配范围小的放上面: (2)不同类规则( ...

  7. git ssh key配置&解决git每次输入密码

    git ssh key配置&解决git每次输入密码:https://blog.csdn.net/qq_42817227/article/details/81415404

  8. HDU 6631 line symmetric(枚举)

    首先能想到的是至少有一对相邻点或者中间间隔一个点的点对满足轴对称,那么接下来只需要枚举剩下的点对是否满足至多移动一个点可以满足要求. 第一种情况,对于所有点对都满足要求,那么Yes. 第二种情况,有一 ...

  9. shutdown的几种方法和利弊

    1.shutdown normal      正常方式关闭数据库. 2.shutdown immediate      立即方式关闭数据库.      在SVRMGRL中执行shutdown imme ...

  10. 生产者消费者模型(JoinableQueue)