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 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]]
Solution: Use HashTable, Time: O(N^2), Space: O(N)
我的:注意14行是有value个重复distance,表示有value个点,他们跟指定点距离都是distance,需要选取2个做permutation, 所以是value * (value-1)
public class Solution {
public int numberOfBoomerangs(int[][] points) {
int res = 0;
for (int i=0; i<points.length; i++) {
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int j=0; j<points.length; j++) {
if (i == j) continue;
int dis = calDistance(points[i], points[j]);
if (!map.containsKey(dis)) map.put(dis, 1);
else map.put(dis, map.get(dis)+1);
}
for (int value : map.values()) {
if (value > 1) {
res += value * (value - 1);
}
}
}
return res;
} public int calDistance(int[] p1, int[] p2) {
int dx = Math.abs(p2[0] - p1[0]);
int dy = Math.abs(p2[1] - p1[1]);
return dx*dx + dy*dy;
}
}
别人的简洁写法
public int numberOfBoomerangs(int[][] points) {
int res = 0; Map<Integer, Integer> map = new HashMap<>();
for(int i=0; i<points.length; i++) {
for(int j=0; j<points.length; j++) {
if(i == j)
continue; int d = getDistance(points[i], points[j]);
map.put(d, map.getOrDefault(d, 0) + 1);
} for(int val : map.values()) {
res += val * (val-1);
}
map.clear();
} return res;
} private int getDistance(int[] a, int[] b) {
int dx = a[0] - b[0];
int dy = a[1] - b[1]; return dx*dx + dy*dy;
}
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 ...
- Python3解leetcode Number of Boomerangs
问题描述: Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple ...
- 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]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&Python] Problem 447. Number of Boomerangs
Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of po ...
- C#LeetCode刷题之#447-回旋镖的数量(Number of Boomerangs)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3792 访问. 给定平面上 n 对不同的点,"回旋镖&q ...
随机推荐
- iOS 评论APP撰写评论
---- iOS 应用评分 UIAlertAction *alertAction1 = [UIAlertAction actionWithTitle:@"方式1 跳转到app商店" ...
- 深入浅出 - Android系统移植与平台开发(二) - 准备Android开发环境
作者:唐老师,华清远见嵌入式学院讲师. 编译Android源码 关于android系统的编译,Android的官方网站上也给出了详细的说明.http://source.android.com/sour ...
- C#反射生成简单sql语句
static void Main(string[] args) { book book = new book();//实体类 booktest b1 = new booktest(); book.bo ...
- mysql 存储过程 删除重复
DELIMITER $$ CREATE PROCEDURE `delRepeatCA`() BEGIN DECLARE tally INT DEFAULT 0; SELECT COUNT(rs.c_C ...
- python生成器
eg1: >>> (i*i for i in range(5))<generator object <genexpr> at 0x16b8fa0>>&g ...
- javascript 变量,作用域,内存管理小结
js的变量保存两种类型的数据——基本数据类型与引用类型.具有以下几点特征: 变量: 1)基本类型值在内存中占固定大小的空间,因此被保存在栈内存中; 2) 把保存基本类型值得变量赋给另一个变量,会创 ...
- mysql syntax bypass some WAF
select{x table_name}from{x information_schema.tables} mysql> select{x table_name}from{x informati ...
- CloudSim样例分析
自带八个样例描述: cloudsim-2.1.1\examples目录下提供了一些CloudSim样例程序,每个样例模拟的环境如下: (1)CloudSimExample1.Java:创建一个一台主机 ...
- super()和this()的区别
1)调用super()必须写在子类构造方法的第一行,否则编译不通过.每个子类构造方法的第一条语句,都是隐含地调用super(),如果父类没有这种形式的构造函数,那么在编译的时候就会报错. 2)supe ...
- Log4J简单使用
一.一般会将commons-logging和Log4j一起使用 原因:1.commons-logging功能较弱 2.log4j功能强大. 所需jar: log4j-1.2.16.ja ...