Description

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

my program

思路:创建points.size()*points.size()的数组distance,存放每个点到各个点的距离的平方(距离的话需要开方,产生了浮点数,这里避免了),对数组的每一行元素进行排序,找出每行中某个元素相同的数目n,然后计数count累加(1+2+...+n)的和

class Solution {
public:int sum(int n) //求1~n的和{int res = 0;while(n){res += n--;}return res;}int numberOfBoomerangs(vector<pair<int, int>>& points) {int count = 0;if(points.size() <= 2) return count;vector<vector<long long> > distance; //points.size()*points.size()的数组//存放每个点到各个点的距离的平方(距离的话需要开方,产生了浮点数,这里避免了)for(int i = 0; i<points.size(); i++)    //初始化数组{vector<long long>temp(points.size(),0);distance.push_back(temp);}for(int i = 0;i<points.size(); i++)     //填充数组(计算距离平方填充){for(int j = 0; j<points.size(); j++){long long x = points[i].first - points[j].first;long long y = points[i].second - points[j].second;distance[i][j] = x*x + y*y;}}for(int i = 0;i<points.size(); i++) {sort(distance[i].begin(),distance[i].end());int j = 0;while(j<points.size()-1){int n = 0;//每行中某个元素相同的数目while(j<points.size()-1 && distance[i][j] == distance[i][j+1]){n++;j++;}if( n != 0) count += sum(n)*2;j++;}}return count;}
};

Submission Details
31 / 31 test cases passed.
Status: Accepted
Runtime: 175 ms
Your runtime beats 92.00% of cpp submissions.

other methods

For each point i, map<distance d, count of all points at distance d from i>.
Given that count, choose 2 (with permutation) from it, to form a boomerang with point i.
[use long appropriately for dx, dy and key; though not required for the given test cases]
Time Complexity: O(n^2)

int numberOfBoomerangs(vector<pair<int, int>>& points) {int res = 0;// iterate over all the pointsfor (int i = 0; i < points.size(); ++i) {unordered_map<long, int> group(points.size());// iterate over all points other than points[i]for (int j = 0; j < points.size(); ++j) {if (j == i) continue;int dy = points[i].second - points[j].second;int dx = points[i].first - points[j].first;// compute squared euclidean distance from points[i]int key = dy * dy;key += dx * dx;// accumulate # of such "j"s that are "key" distance from "i"++group[key];}for (auto& p : group) {if (p.second > 1) {/** for all the groups of points, * number of ways to select 2 from n = * nP2 = n!/(n - 2)! = n * (n - 1)*/res += p.second * (p.second - 1);}}}return res;
}
int numberOfBoomerangs(vector<pair<int, int>>& points) {int booms = 0;for (auto &p : points) {unordered_map<double, int> ctr(points.size());for (auto &q : points)booms += 2 * ctr[hypot(p.first - q.first, p.second - q.second)]++;}return booms;
}

Try each point as the “axis” of the boomerang, i.e., the “i” part of the triple. Group its distances to all other points by distance, counting the boomerangs as we go. No need to avoid q == p, as it’ll be alone in the distance == 0 group and thus won’t influence the outcome.

Submitted five times, accepted in 1059, 1022, 1102, 1026 and 1052 ms, average is 1052.2 ms. The initial capacity for ctr isn’t necessary, just helps make it fast. Without it, I got accepted in 1542, 1309, 1302, 1306 and 1338 ms.

转载于:https://www.cnblogs.com/yangjiannr/p/7391348.html

LeetCode447. Number of Boomerangs相关推荐

  1. 447. Number of Boomerangs

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

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

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

  3. LeetCode Number of Boomerangs

    题意:给出平面上的n个点,求出元组(i,j,k)的个数,其中元组满足i与j的距离与i与k距离相等,与(i,j,k)顺序有关 思路:以一个点为起点,看其它点到该点的距离及具有一样距离的个数.在统计元组个 ...

  4. C#LeetCode刷题-哈希表

    哈希表篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 42.8% 简单 3 无重复字符的最长子串   24.2% 中等 18 四数之和   ...

  5. LeetCode 简单算法题

    使用Nodejs 抓取的LeetCode 简单算法题  一步一步来,先攻破所有简单的题目,有些题目不适合使用JS解决,请自行斟酌 Letcode 简单题汇总 104. Maximum Depth of ...

  6. LeetCode 分类练习(四):查找2

    LeetCode 分类练习(四):查找2 目录 LeetCode 分类练习(四):查找2 一.双指针(快慢指针.对撞指针) 1.快慢指针 2.对撞指针 3.滑动窗口法 二.滑动数组 三.实战1(对撞指 ...

  7. LeetCode github集合,附CMU大神整理笔记

    Github LeetCode集合 本人所有做过的题目都写在一个java项目中,同步到github中了,算是见证自己的进步.github目前同步的题目是2020-09-17日之后写的题.之前写过的题会 ...

  8. 玩转算法面试LeetCode题目目录

    文章目录 一.数组中的问题其实最常见 1. 如何写出正确的程序 2. 基础算法思路的应用 3. 对撞指针 4. 滑动窗口 二.查找表相关问题 三.在链表中穿针引线 四.栈.队列.优先队列 1. 栈的基 ...

  9. LeetCode 力扣算法题解汇总,All in One

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: https://fuxuemingzhu.cn 关键词:LeetCode,力扣,算法,题解,汇总,解析 把自己刷过的所有题目做一个整理, ...

最新文章

  1. 求职技巧—2014六个秘诀二 - 年求职
  2. APUE读书笔记-12线程控制-04同步属性
  3. RedHat下建立群集
  4. 《零基础看得懂的C语言入门教程 》——(五)C语言的变量、常量及运算
  5. SoapUI 5.4.0 中文乱码
  6. Spring框架是如何判断是否是上传文件请求呢
  7. 广告传媒实际税负怎么计算_建材销售类营业额3亿,缺进项致税负高?成立4家独资企业节税90%...
  8. 结对作业_core组
  9. AS3和Flex常用知识100条
  10. max3232ese_max3232中文资料汇总(max3232引脚功能图_特性参数及应用电路)
  11. ArcGIS批量计算图层中矢量要素面积——ArcMap
  12. java代码教程_【B0609】[java视频教程]高效编程-代码精进之路2019视频教程 it教程...
  13. 计算机桌面文件夹不显示不出来的,如何隐藏文件夹别人都看不到
  14. 关于Dev C++等软件突然提示16位应用程序不兼容的问题
  15. 文本对比,文本差异并排对比显示实现
  16. css绘制等边三角形
  17. Git常用命令、及常见报错处理:You have not concluded your merge (MERGE_HEAD exists)
  18. Qtum量子链研究院:Qtum Plasma MVP 技术详解
  19. [Erlang] XML处理方案
  20. 搜狗输入法截屏工具的使用

热门文章

  1. SQL Server 函数的使用(数学函数)
  2. matlab 求二值图像图形的面积和重心
  3. tshark/wireshark抓包小结
  4. python机器学习案例系列教程——CTR/CVR中的FM、FFM算法
  5. OPNET网络仿真分析-1.3、基础概念
  6. 帮助浏览器、help函数和doc函数 符号表述的数集
  7. 虹软AI 人脸识别SDK接入 — 性能优化篇(多线程)
  8. mybatis框架增删改的注意事项 ,不同数据库取消转义的方法
  9. centos7 yum安装mysql后启动不起来问题
  10. SSM框架及例子(转)