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 ...
随机推荐
- 51nod算法马拉松13
A 取余最长路 不难发现路径可以拆成三条线段,只要知道两个转折点的位置就能计算出答案. 设sum(i,l,r)表示第i行从l到r元素的和,则答案可以表示为sum(1,1,x)+sum(2,x,y)+s ...
- Jquery_AjaxFileUpload插件的使用记录
功能需求:Spring MVC框架下,实现无刷新页面上传图片,并展示图片预览效果 直接上代码: 1.图片预览效果 <%@ page contentType="text/html;cha ...
- 20145330《Java程序设计》第三周学习总结
20145330 <Java程序设计>第三周学习总结 第三周知识的难度已经逐步上升,并且一周学习两章学习压力也逐渐加大,需要更高效率的来完成学习内容,合理安排时间. 类与对象 对象(Obj ...
- ECLIPSE下SVN的创建分支/合并/切换使用
最近接项目要求,要在svn主干上创建分支,用分支来进行程序的bug修改,而主干上进行新功能的开发.分支上的bug修改完,发布后,可以合并到主干上.项目程序可以在主干和分支之间进行切换,来实现主干和分支 ...
- iOS五种本地缓存数据方式
iOS五种本地缓存数据方式 iOS本地缓存数据方式有五种:前言 1.直接写文件方式:可以存储的对象有NSString.NSArray.NSDictionary.NSData.NSNumber,数据 ...
- JSP内置对象---request和 response
<%@page import="java.net.URLEncoder"%> <%@page import="com.hanqi.web.CardDAO ...
- Js的引用关系示例和总结
三种引用(指针引用)关系,借助引用关系可以形成复杂的链关系,巧妙借助链关系可以实现收放自如,形散神不散的神奇效果,jquery就是其中一例: 1.对象指向属性; 2.a=b(b是对象,a ...
- Daily Scrum 10.29
今天大家的工作做的还算不错,但是晚些时候遗憾的得知我们的吴文会同学生病住院了,所以她明天的任务暂时保留,再做调整.希望大家在努力学习工作的同时一定要注意身体啊! 下面是今天的Task统计:
- js 时间格式化 -- 时间加减实现
时间格式化的方法: Date.prototype.Format = function (fmt) { //author: meizz var o = { "M+": this.ge ...
- C# 文件和文件夹操作
一.文件操作 1.File类的常用静态方法: void AppendAllText(string path, string contents),将文本contents附加到文件path中 bool E ...