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 iand 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:

  1. Input:
  2. [[0,0],[1,0],[2,0]]
  3.  
  4. Output:
  5. 2
  6.  
  7. Explanation:
  8. The two boomerangs are [[1,0],[0,0],[2,0]] and [[1,0],[2,0],[0,0]]
  9.  
  10. 解法:
    双重循环,记录每个点和其他的点的距离。
  1. public int numberOfBoomerangs(int[][] points) {
  2. int result = 0;
  3. for(int i = 0; i< points.length; i++){
  4. Map<Integer,Integer> hash = new HashMap<>();
  5. for( int j = 0; j< points.length; j++){
  6. if(i == j){
  7. continue;
  8. }else{
  9. int dist = getDistance(points[i],points[j]);
  10. hash.put(dist,hash.getOrDefault(dist,0)+1);
  11. }
  12. }
  13. for(Integer val : hash.values()){
  14. result += val * (val - 1);
  15. }
  16. }
  17. return result;
  18. }
  19.  
  20. int getDistance(int[] p1, int[] p2){
  21. int x = p1[0] - p2[0];
  22. int y = p1[1] - p2[1];
  23. return x*x + y*y;
  24. }

[LeetCode]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. 34. 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】447. Number of Boomerangs 解题报告(Java & Python)

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

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

  5. 447. Number of Boomerangs

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

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

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

  7. 【leetcode】447. Number of Boomerangs

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

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

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

  9. [刷题] 447 Number of Boomerangs

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

随机推荐

  1. 解决:jmeter查看结果树的响应数据提示超过最大值Response too large to be displayed

    问题:用jmeter做接口测试时,查看结果树的数据没有显示全,给出下面的错误提示 Response too large to be displayed. Size: 1349830 > Max: ...

  2. 日常维护sql

    修复mysqlcheck -u -p --repair  pmdb prefix="/export/data/mysql/bin/mysql -u -p -e" domain=机房 ...

  3. python Day01

    Python Day01 Python 简介 介绍 Python 是一种面向对象.直译式的计算机程序设计语言,也是一种功能强大的通用型语言,已经有将近二十年的发展历史,成熟稳定.包含了一组完善而且容易 ...

  4. Linux Svn 安装过程及配置

    重要的是第一步的安装,第二步配置可能没用,但是没试过,因为服务器上已经安装了第一步. 此处的第二步只为做个记录,说明一下里边的配置文件的用途. 3. 自己实际操作中的的配置记录(参照服务器别人的配置记 ...

  5. python——请求服务器(http请求和https请求)

    一.http请求 1.http请求方式:get和post get一般用于获取/查询资源信息,在浏览器中直接输入url+请求参数点击enter之后连接成功服务器就能获取到的内容,post请求一般用于更新 ...

  6. Quickly place a window to another screen using only the keyboard

    http://askubuntu.com/questions/22207/quickly-place-a-window-to-another-screen-using-only-the-keyboar ...

  7. ruby Matrix 输出 格式化

    require 'matrix' class Matrix def to_pretty_s s = "" i = 0 while i < self.column_size s ...

  8. Android开发工具全面转向Android Studio(2)——AS project/module的CRUD

    本文有些地方可能需要衔接Android开发工具全面转向Android Studio(1)——准备开发环境,读起来效果会更好. 这个世界很奇妙,所有的东西离不开CRUD,即增删改查.即使人本身也遵循这个 ...

  9. Ubuntu 试用Android L版本

    Android L是最近google一个大更新的版本,目前google开发了android L的开发者预览版本,对于一个android 开发者来说很定是要下载下来体验一把,顺便也要了解一下Androi ...

  10. jQuery将悬停效果加到菜单项

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...