问题描述:

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. element-ui走马灯如何实现图片自适应 长度和高度 自适应屏幕大小

    最近写用vue2.0写一个项目,用到了走马灯效果,由于项目赶时间,想偷下懒,第一次引用了element组件(纯JS也可以写的出来,赶时间嘛,懂得....),结果用了发现一个问题,element的组件( ...

  2. Mac 10.14 下为php 安装xdebug 并让vscode支持

    安装Xdebug 寻找对应php版本的xdebug版本 先将info输出到一个文件 php -i > info.txt 打开info.txt 复制所有内容 打开寻找合适xdebug的页面http ...

  3. python正则表达式整理

    正则表达式在处理字符串时很大的作用,爬虫中也经常用到,下面就将一些常用正则表达式做一整理记录,方便以后查看. ^d   表示匹配以d开头的字符串 .      表示匹配任意字符串 *     表示前面 ...

  4. 关于时间API

    如何正确处理时间 现实生活的世界里,时间是不断向前的,如果向前追溯时间的起点,可能是宇宙出生时,又或是宇宙出现之前, 但肯定是我们目前无法找到的,我们不知道现在距离时间原点的精确距离.所以我们要表示时 ...

  5. [Python3 填坑] 017 实例方法、静态方法、类方法的区别

    目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 先上例子 2.2 分析 1. print( 坑的信息 ) 挖坑时间:2019/04/07 明细 坑的编码 内容 Py024-1 实例方法 ...

  6. [Vim] 03 凡人进阶

    目录 0. 前言 1. 按下 Esc, 进入编辑模式 (1) 定位 (2) 删除 1) 不进入插入模式的删除 2) 进入插入模式的删除 3. 10 个特殊字符 4. 在 gVim 下执行命令 (1) ...

  7. (4.30)全面了解触发器:DML、DDL、LOGON触发器

    DML.DDL.LOGON触发器 转自:https://www.cnblogs.com/seusoftware/p/9120632.html 触发器可以理解为由特定事件触发的存储过程, 和存储过程.函 ...

  8. MySQL-第十四篇事务管理

    1.什么是事务 事务是由一步或者几步数据库操作序列组成的逻辑执行单元,这系列操作要么全部执行,要么全部放弃执行. 2.事务具备的4个特性: 1>原子性(Atomicity):事务是应用中最小的执 ...

  9. (六:NIO系列) 相关设计模式

    出处:  反应器模式 vs 观察者模式  反应器模式 vs 生产者消费者模式 反应器模式 vs 观察者模式 反应器模式 是一种为处理服务请求并发提交到一个或者多个服务处理程序的事件设计模式.当请求抵达 ...

  10. C#控制文本框(TextBox)只能输入正数,负数,小数

    由于项目需要,需要写一个TextBox文本框,此文本框需要满足:只能输入正数,负数和小数.比如:3,0.3,-4,-0.4等等.        在网上找了许多正则表达式都不好用,由于本人又对正则表达式 ...