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的更多相关文章

  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. Python3解leetcode Number of Boomerangs

    问题描述: Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple ...

  4. LeetCode 447. Number of Boomerangs (回力标的数量)

    Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of po ...

  5. 【LeetCode】447. Number of Boomerangs 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 [LeetCode] 题目地址:https:/ ...

  6. [LeetCode]447 Number of Boomerangs

    Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of po ...

  7. 34. leetcode 447. Number of Boomerangs

    Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of po ...

  8. [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 ...

  9. C#LeetCode刷题之#447-回旋镖的数量(Number of Boomerangs)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3792 访问. 给定平面上 n 对不同的点,"回旋镖&q ...

随机推荐

  1. To Do List

    妈呀...发现不发博文公布自己要学的东西压力少太多了.......... 然后就会变得颓废..................... 求大家监督QAQ....To Do List是近3天左右目标,3天 ...

  2. C#文字样式

    [字体] 中文名称 英文名称宋体 SimSun黑体 SimHei微软雅黑 Microsoft YaHei微软正黑体   Microsoft JhengHei新宋体   NSimSun新细明体 PMin ...

  3. SpringMVC+Thymeleaf如何处理URL中的动态查询参数

    1.使用一个Map<String, String>接收数据 When you request a Map annotated with @RequestParam Spring creat ...

  4. Go语言练习:网络编程实例——简易图片上传网站

    1.代码结构 2.运行实例 1.代码结构 $ tree . ├── photoweb.go ├── public │   ├── css │   ├── images │   └── js ├── u ...

  5. jsp页面价格

    <div class='flo_left'> <h2 class='red'>订货价¥${productEntity.agentsPrice}</h2> <h ...

  6. display:inline、block、inline-block 的区别

    一.块级元素 与  行级元素 要理解display:inline.block.inline-block的区别,需要先了解HTML中的块级(block)元素和行级(inline)元素的特点,行内元素也叫 ...

  7. [LintCode] Add Binary 二进制数相加

    Given two binary strings, return their sum (also a binary string). Have you met this question in a r ...

  8. MySQL中引号的问题

    原文: http://blog.csdn.net/wisgood/article/details/6317543 mysql中一个字符串,既可以用两个单引号表示,也可以用两个双引号表示. 比如字符串 ...

  9. firefox如何禁用JavaScript

    1.在Firefox地址栏里输入“about:config”.2.在搜索栏输入“javascript.enabled”查找到首选项.3.点击鼠标右键选择“切换”,把“javascript.enable ...

  10. jQuery -> 获取元素的各种过滤器

    转自http://blog.csdn.net/feelang/article/details/26613023