[抄题]:

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]]

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:n^2

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

不知道怎么表示距离相等:用hashmap统计距离的出现次数

[一句话思路]:

用hashmap统计距离的出现次数,再用组合公式选2个点

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 理解“求距离”的两点的地位是等价的。所以范围相同,都是<length
  2. 用getordefault的时候还是要 提前指定default类型的,稍微注意下

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

“距离相等”可以理解为“同一距离出现了两次”,用hashmap

[复杂度]:Time complexity: O(n^2 双重循环) Space complexity: O(n)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

不知道距离怎么表示:a[0] - b[0], a[1] - b[1] 平方和

[关键模板化代码]:

求距离:

public int getDistance(int[] a, int[] b) {
int dx = a[0] - b[0];
int dy = a[1] - b[1]; return dx * dx + dy * dy;
}

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

class Solution {
public int numberOfBoomerangs(int[][] points) {
//cc
if (points == null || points[0] == null) {
return 0;
} //ini hashmap
HashMap<Integer, Integer> map = new HashMap<>();
int res = 0; //for loop: put into map
//res += val * (val - 1)
//.clear()
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
return res;
} public int getDistance(int[] a, int[] b) {
int dx = a[0] - b[0];
int dy = a[1] - b[1]; return dx * dx + dy * dy;
}
}

447. Number of Boomerangs 回力镖数组的数量的更多相关文章

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

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

  2. 447. Number of Boomerangs

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

  3. [LeetCode]447 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. [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 ...

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

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

  7. 447 Number of Boomerangs 回旋镖的数量

    给定平面上 n 对不同的点,“回旋镖” 是由点表示的元组 (i, j, k) ,其中 i 和 j 之间的距离和 i 和 k 之间的距离相等(需要考虑元组的顺序).找到所有回旋镖的数量.你可以假设 n ...

  8. 【leetcode】447. Number of Boomerangs

    题目如下: 解题思路:我首先用来时间复杂度是O(n^3)的解法,会判定为超时:后来尝试O(n^2)的解法,可以被AC.对于任意一个点,我们都可以计算出它与其余点的距离,使用一个字典保存每个距离的点的数 ...

  9. [刷题] 447 Number of Boomerangs

    要求 给出平面上n个点,寻找存在多少点构成的三元组(i j k),使得 i j 两点的距离等于 i k 两点的距离 n 最多为500,所有点坐标范围在[-10000, 10000]之间 示例 [[0, ...

随机推荐

  1. (三十七)js改变this指向的方法

    最近又遇到了JacvaScript中的call()方法和apply()方法,而在某些时候这两个方法还确实是十分重要的,那么就让我总结这两个方法的使用和区别吧. 1.改变函数内部的this指向的三种方法 ...

  2. squid http_access中的逻辑关系

    http_access通过acl实现访问控制,方法 acl A acltype argument acl B acltype argument 逻辑关系:或 http_access allow|den ...

  3. CentOS 6.5添加网易163源

    换国内的yum源. 准备工作,首先备份/etc/yum.repos.d/CentOS-Base.repo              cd /etc/yum.repos.d/ wget http://m ...

  4. Microsoft Office 2013 (64位) 免费完整版(安装 + 激活)

    Microsoft Office 2013(Office 15)是微软的新一代Office办公软件,全面采用Metro界面.Microsoft Office 2013官方下载(Office2013专业 ...

  5. bzoj 2435 道路修建

    Written with StackEdit. Description 在 \(W\) 星球上有 \(n\) 个国家.为了各自国家的经济发展,他们决定在各个国家 之间建设双向道路使得国家之间连通.但是 ...

  6. 由于出现操作系统错误 3,进程无法读取文件D:\XXXX\X.pre (源: MSSQL_REPL,错误号: MSSQL_REPL20024)

    最近着手做SqlServer2008的订阅发布,起初使用推送订阅很顺利,后来改成请求订阅出现了以下问题,折腾好长时间终于搞定,留下此文备日后查阅,或供遇相同问题的道友参考: 首先阐述以下问题: 1. ...

  7. haproxy中两个常用的宏

    list_for_each(pos, head) pos.head 都为list类型 一般来说,head是list的虚拟头,依次取list上的各个节点 container_of(ptr, type, ...

  8. (转)用Eclipse 统计代码行数小技巧

    今天公司SQA问我目前项目代码行数有多少,我当时就是想,以前好像写过类似的统计工具但是一时又找不到 公司网络又不能下载,所以想想eclipse是不是又类似功能,找了下没有,但突然一想有一个转弯方法:统 ...

  9. PHP echo汉字出现乱码的情况之一

    当然首先要理清楚到底是web服务端造成,php造成的,还是数据库造成的. 需要在头文件里UTF-8 <head> <meta http-equiv="Content-Typ ...

  10. android开源库收集

    1. google valley Android网络通信库 git clone https://android.googlesource.com/platform/frameworks/volley ...