447. Number of Boomerangs

Easy

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]]
package leetcode.easy;

public class NumberOfBoomerangs {
public int numberOfBoomerangs(int[][] points) {
int res = 0;
java.util.HashMap<Integer, Integer> map = new java.util.HashMap<Integer, Integer>();
for (int[] i : points) {
for (int[] j : points) {
if (i == j) {
continue;
}
Integer dist = (i[0] - j[0]) * (i[0] - j[0]) + (i[1] - j[1]) * (i[1] - j[1]);
Integer prev = map.get(dist);
if (prev != null) {
res += 2 * prev;
}
map.put(dist, (prev == null) ? 1 : prev + 1);
}
map.clear();
}
return res;
} @org.junit.Test
public void test() {
int[][] points = { { 0, 0 }, { 1, 0 }, { 2, 0 } };
System.out.println(numberOfBoomerangs(points));
}
}

LeetCode_447. Number of Boomerangs的更多相关文章

  1. [LeetCode] Number of Boomerangs 回旋镖的数量

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

  2. [LeetCode]447 Number of Boomerangs

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

  3. Leetcode: Number of Boomerangs

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

  4. 34. leetcode 447. Number of Boomerangs

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

  5. 447. Number of Boomerangs

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

  6. [Swift]LeetCode447. 回旋镖的数量 | Number of Boomerangs

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

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

  8. LeetCode——Number of Boomerangs

    LeetCode--Number of Boomerangs Question Given n points in the plane that are all pairwise distinct, ...

  9. 447. Number of Boomerangs 回力镖数组的数量

    [抄题]: Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple ...

随机推荐

  1. git及github使用

    1.git安装 widows: 可以去git官网下载安装 ubantu : 使用命令 sudo apt-get install git 进行安装 2.git启动 widows: 首先去到需要建立git ...

  2. C++——STL(算法)

    以下对所有算法进行细致分类并标明功能:<一>查找算法(13个):判断容器中是否包含某个值adjacent_find:   在iterator对标识元素范围内,查找一对相邻重复元素,找到则返 ...

  3. Multi-Task Feature Learning for Knowledge Graph Enhanced Recommendation(知识图谱)

    知识图谱(Knowledge Graph,KG)可以理解成一个知识库,用来存储实体与实体之间的关系.知识图谱可以为机器学习算法提供更多的信息,帮助模型更好地完成任务. 在推荐算法中融入电影的知识图谱, ...

  4. 2019年牛客多校第一场 C题Euclidean Distance 暴力+数学

    题目链接 传送门 题意 给你\(n\)个数\(a_i\),要你在满足下面条件下使得\(\sum\limits_{i=1}^{n}(a_i-p_i)^2\)最小(题目给的\(m\)只是为了将\(a_i\ ...

  5. KVM-virsh常用命令

    virsh list #在线VM virsh list --all #所有VM virsh start #开机 virsh shutdown #软关机 virsh destroy #强制关机 virs ...

  6. Spark数据倾斜解决方案(转)

    本文转发自技术世界,原文链接 http://www.jasongj.com/spark/skew/ Spark性能优化之道——解决Spark数据倾斜(Data Skew)的N种姿势  发表于 2017 ...

  7. 清除DNS缓存和刷新DHCP列表

    ipconfig /release 只是释放IP地址,然后还需要ipconfig /renew在重新获取一下 如何清除DNS缓存?开始-运行,如下图所示: 在谈出的对话框中输入“cmd”,如下图所示: ...

  8. (转)接口测试工具Postman使用实践

    一.接口定义 软件不同部分之间的交互接口.通常就是所谓的API――应用程序编程接口,其表现的形式是源代码. —— [ 百度百科 ]我们常说的接口一般指两种:(1)API:应用程序编程接口.程序间的接口 ...

  9. BBS总结 --- django设计

    目录 1.调用模块使用 2.BBS中urls.py 3.django中配置 4.新学方法使用 5.BBS用到的知识点 1.调用模块使用 from django.db import models fro ...

  10. tox 试用

    安装 pip install tox tox 使用 tox 包含一个tox.ini 文件,此文件可以自动,或者手工编写 tox.ini 文件格式 # content of: tox.ini , put ...