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:

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]] 解法:
双重循环,记录每个点和其他的点的距离。
public int numberOfBoomerangs(int[][] points) {
int result = 0;
for(int i = 0; i< points.length; i++){
Map<Integer,Integer> hash = new HashMap<>();
for( int j = 0; j< points.length; j++){
if(i == j){
continue;
}else{
int dist = getDistance(points[i],points[j]);
hash.put(dist,hash.getOrDefault(dist,0)+1);
}
}
for(Integer val : hash.values()){
result += val * (val - 1);
}
}
return result;
} int getDistance(int[] p1, int[] p2){
int x = p1[0] - p2[0];
int y = p1[1] - p2[1];
return x*x + y*y;
}

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

    [CoreSeek] CoreSeek有两个核心模块Indexer和Search. Indexer:负责从MySQL拉取数据源,把数据源分词,建立索引. Search:搜索模块. CoreSeek工作 ...

  2. wcf用svcutil导出泛型的元数据

    D:\aaa>svcutil net.tcp://192.168.1.110:44444/TradingsService.svc/mex /ct:System.Collections.Gener ...

  3. cocos2dx中常见设计模式

    1.单例设计模式:导演类 2.观察者模式: 被观察者含有一个数组,里边存放了所有观察者的引用,在被观察者的状态发生改变的时候,通过调用观察者的函数来通知观察者,实现了信息的传递.  事件监听器:就是采 ...

  4. 在GMIC听“移动互联网+医疗”的感受 2015-04-29

    “互联网+”这个词挺火的,基本格式是“互联网+传统行业”,比如医疗.教育.交通等等.就更别说电子商务.金融这些领域了,相比已经和互联网分不 开了.在我看来,互联网+的背后,是信息化.智能化.信息沟通的 ...

  5. 纯js异步无刷新请求(只支持IE)

    纯js异步无刷新请求 下载地址:http://pan.baidu.com/s/1slakL1F 所以因为非IE浏览器都禁止跨域请求,所以以只支持IE. <HTML> <!-- 乱码( ...

  6. man curl_easy_perform(原创)

    curl_easy_perform(3)           libcurl 手册                  curl_easy_perform(3) 名字 curl_easy_perform ...

  7. 从H264码流中获取视频宽高 (SPS帧) 升级篇

    之前写过 <从H264码流中获取视频宽高 (SPS帧)> . 但发现很多局限性,而且有时解出来是错误的. 所以重新去研究了. 用了 官方提供的代码库来解析. 花了点时间,从代码库里单独把解 ...

  8. WebMethod在webservice里面非静态方法能调用,在页面类里面,静态方法才能调用

    WebMethod在webservice里面非静态方法能调用,在页面类里面,静态方法才能调用

  9. jquery验证

    首先要引用js库 <script src="js/jquery-1.7.2.min.js"></script> jquery验证方式 function ch ...

  10. CSS text-transform 属性

    text-transform 属性控制文本的大小写. h1 {text-transform:uppercase} h2 {text-transform:capitalize} p {text-tran ...