Python3解leetcode 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 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的更多相关文章
- LeetCode——Number of Boomerangs
LeetCode--Number of Boomerangs Question Given n points in the plane that are all pairwise distinct, ...
- [LeetCode] Number of Boomerangs 回旋镖的数量
Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of po ...
- Leetcode: Number of Boomerangs
Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of po ...
- Python3解leetcode Reach a Number
问题描述: You are standing at position 0 on an infinite number line. There is a goal at position target. ...
- Python3解leetcode Single Number
问题描述: Given a non-empty array of integers, every element appears twice except for one. Find that sin ...
- 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 ...
- Python3解leetcode Maximum Subarray
问题描述: Given an integer array nums, find the contiguous subarray (containing at least one number) whi ...
- Python3解leetcode Count Binary Substrings
问题描述: Give a string s, count the number of non-empty (contiguous) substrings that have the same numb ...
- Python3解leetcode First Bad Version
问题描述: You are a product manager and currently leading a team to develop a new product. Unfortunately ...
随机推荐
- Powershell&TFS_Part 1
目录 目录 前言 TFS 对象模型 Powershell Powershell面向对象 Powershell默认会在PC中设置执行脚本权限 调试脚本 断点 Step Microsoft Visual ...
- 阶段1 语言基础+高级_1-3-Java语言高级_1-常用API_1_第5节 String类_2_字符串的构造方法和直接创建
string的构造方法 psvm创建main方法 把字节翻译成了小a小b小c.字符串的底层科室用的byte字节数组 Ctrl+鼠标左键点击string 这个byte就保存了字符串底层的字节数据 直接创 ...
- QTP 11 补丁大全
原文: http://relevantcodes.com/qtp-11-0-patches/ Patch Link Details Support for Chrome 19 QTPWEB_00102 ...
- c# Thread——1.为什么Abort中断线程是不可靠的
Thread.Abort 方法在c#中用作强制中断线程的执行,大多用于线程内部满足某个特定条件而自己调用关闭自身,比如下面的代码在i自增到3的时候就会停止打印. class Program { sta ...
- SSM001/构建maven多模块项目
一.Idea构建maven多模块项目 1.创建maven项目--创建父模块 [1].File->New->Module... [2].点击next,填写:GroupId,ArtifactI ...
- jQ全选或取消全选
function checkAll(chkobj) { if ($(chkobj).children("span").text() == "全选" ...
- Vue ----》 如何实现 sessionStorage 的监听,实现数据响应式
在开发过程中,组件中的随时可能改变的数据有的是缓存到sessionStorage里面的,但是有些组件取seesionStorage中的值时,并不能取到更新后的值. 接下来就说一下,当seesionSt ...
- javascript 阻止事件冒泡
阻止冒泡 冒泡简单的举例来说,儿子知道了一个秘密消息,它告诉了爸爸,爸爸知道了又告诉了爷爷,一级级传递从而引起事件的混乱,而阻止冒泡就是不让儿子告诉爸爸,爸爸自然不会告诉爷爷了. 举个栗子: 父容器是 ...
- 【洛谷p1158】导弹拦截
这道题是个有想法的枚举qwq 导弹拦截[题目链接] 注意:此导弹拦截非彼导弹拦截p1020 导弹拦截 一道题是1999年的,然后我们现在要写的是经过11年韬光养晦之后的导弹拦截 SOLUTION: 要 ...
- [2019杭电多校第二场][hdu6598]Harmonious Army(最小割)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6598 题意是说一个军队有n人,你可以给他们每个人安排战士或者法师的职业,有m对人有组合技,组合技的信息 ...