[LeetCode]447 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 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:
2 Explanation:
The two boomerangs are [[1,0],[0,0],[2,0]] and [[1,0],[2,0],[0,0]] 解法:
双重循环,记录每个点和其他的点的距离。
public int numberOfBoomerangs(int[][] points) {
int result = 0;
for(int i = 0; i< points.length; i++){
Map<Integer,Integer> hash = new HashMap<>();
for( int j = 0; j< points.length; j++){
if(i == j){
continue;
}else{
int dist = getDistance(points[i],points[j]);
hash.put(dist,hash.getOrDefault(dist,0)+1);
}
}
for(Integer val : hash.values()){
result += val * (val - 1);
}
}
return result;
} int getDistance(int[] p1, int[] p2){
int x = p1[0] - p2[0];
int y = p1[1] - p2[1];
return x*x + y*y;
}
[LeetCode]447 Number of Boomerangs的更多相关文章
- LeetCode 447. Number of Boomerangs (回力标的数量)
Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of po ...
- 34. leetcode 447. Number of Boomerangs
Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of po ...
- 【LeetCode】447. Number of Boomerangs 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 [LeetCode] 题目地址:https:/ ...
- [LeetCode&Python] Problem 447. Number of Boomerangs
Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of po ...
- 447. Number of Boomerangs
Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of po ...
- 447. Number of Boomerangs 回力镖数组的数量
[抄题]: Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple ...
- 【leetcode】447. Number of Boomerangs
题目如下: 解题思路:我首先用来时间复杂度是O(n^3)的解法,会判定为超时:后来尝试O(n^2)的解法,可以被AC.对于任意一个点,我们都可以计算出它与其余点的距离,使用一个字典保存每个距离的点的数 ...
- 447 Number of Boomerangs 回旋镖的数量
给定平面上 n 对不同的点,“回旋镖” 是由点表示的元组 (i, j, k) ,其中 i 和 j 之间的距离和 i 和 k 之间的距离相等(需要考虑元组的顺序).找到所有回旋镖的数量.你可以假设 n ...
- [刷题] 447 Number of Boomerangs
要求 给出平面上n个点,寻找存在多少点构成的三元组(i j k),使得 i j 两点的距离等于 i k 两点的距离 n 最多为500,所有点坐标范围在[-10000, 10000]之间 示例 [[0, ...
随机推荐
- Java堆、栈和常量池以及相关String的详细讲解(经典中的经典) (转)
原文链接 : http://www.cnblogs.com/xiohao/p/4296088.html 一:在JAVA中,有六个不同的地方可以存储数据: 1. 寄存器(register). 这是最快的 ...
- 在simplescalar中添加基于PISA架构的指令
用sim-safe工具进行模拟.最近看代码的一点点心得记录一下. sim-safe工具:不检查所有指令错误,检查内存对齐和所有内存操作的内存访问权限 程序从main.c的main()函数中进入 int ...
- 调用JavaScript
当webdriver 遇到没法完成的操作时,笔者可以考虑借用JavaScript 来完成,比下下面的例子,通过JavaScript 来隐藏页面上的元素.除了完成webdriver 无法完成的操作,如果 ...
- 调用别人提供的WebService
在开发过程中,许多时候需要使用到别人提供的WebService接口,使用其中的方法. 在调用别人提供的接口时,会得到接口使用的文档,其中包括接口的网络地址及方法作用等. 找到WebService的ws ...
- Serializable接口和transient关键字
1. 什么是Serializable接口? 当一个类实现了Serializable接口(该接口仅为标记接口,不包含任何方法),表示该类可以被序列化. 序列化的目的是将一个实现了Serializable ...
- 使用js把json字符串转为js对象的方法
ECMA-262(E3) 中没有将JSON概念写到标准中,还好在 ECMA-262(E5)中JSON的概念被正式引入了,包括全局的JSON对象和Date的toJSON方法. 1,eval方式解析,恐怕 ...
- hdu 1299 Diophantus of Alexandria (数论)
Diophantus of Alexandria Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
- Android之HttpURLConnection
1.HttpURLConnection连接URL1)创建一个URL对象 URL url = new URL(); 2)利用HttpURLConnection对象从网络中获取网页数据 HttpURLCo ...
- phpstorm设置
phpstorm版本为10.0.3,设置自动换行如下: 快捷方式: 打开新的文件:ctrl+shift+N 格式化:ctrl+alt+L 全局搜索:ctrl+shift+F 更换默认快捷键如下,其实右 ...
- Gradle笔记系列(一)
1.Gradle概述 Gradle是一个基于Apache Ant和Apache Maven概念的项目自动化构建工具.它使用一种基于Groovy的特定领域语言(DSL)来声明项目设置,抛弃了基于XML的 ...