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)的和

  1. class Solution {
  2. public:
  3. int sum(int n) //求1~n的和
  4. {
  5. int res = 0;
  6. while(n)
  7. {
  8. res += n--;
  9. }
  10. return res;
  11. }
  12. int numberOfBoomerangs(vector<pair<int, int>>& points) {
  13. int count = 0;
  14. if(points.size() <= 2) return count;
  15. vector<vector<long long> > distance;
  16. //points.size()*points.size()的数组
  17. //存放每个点到各个点的距离的平方(距离的话需要开方,产生了浮点数,这里避免了)
  18. for(int i = 0; i<points.size(); i++) //初始化数组
  19. {
  20. vector<long long>temp(points.size(),0);
  21. distance.push_back(temp);
  22. }
  23. for(int i = 0;i<points.size(); i++) //填充数组(计算距离平方填充)
  24. {
  25. for(int j = 0; j<points.size(); j++)
  26. {
  27. long long x = points[i].first - points[j].first;
  28. long long y = points[i].second - points[j].second;
  29. distance[i][j] = x*x + y*y;
  30. }
  31. }
  32. for(int i = 0;i<points.size(); i++)
  33. {
  34. sort(distance[i].begin(),distance[i].end());
  35. int j = 0;
  36. while(j<points.size()-1)
  37. {
  38. int n = 0;
  39. //每行中某个元素相同的数目
  40. while(j<points.size()-1 && distance[i][j] == distance[i][j+1])
  41. {
  42. n++;
  43. j++;
  44. }
  45. if( n != 0) count += sum(n)*2;
  46. j++;
  47. }
  48. }
  49. return count;
  50. }
  51. };

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)

  1. int numberOfBoomerangs(vector<pair<int, int>>& points) {
  2. int res = 0;
  3. // iterate over all the points
  4. for (int i = 0; i < points.size(); ++i) {
  5. unordered_map<long, int> group(points.size());
  6. // iterate over all points other than points[i]
  7. for (int j = 0; j < points.size(); ++j) {
  8. if (j == i) continue;
  9. int dy = points[i].second - points[j].second;
  10. int dx = points[i].first - points[j].first;
  11. // compute squared euclidean distance from points[i]
  12. int key = dy * dy;
  13. key += dx * dx;
  14. // accumulate # of such "j"s that are "key" distance from "i"
  15. ++group[key];
  16. }
  17. for (auto& p : group) {
  18. if (p.second > 1) {
  19. /*
  20. * for all the groups of points,
  21. * number of ways to select 2 from n =
  22. * nP2 = n!/(n - 2)! = n * (n - 1)
  23. */
  24. res += p.second * (p.second - 1);
  25. }
  26. }
  27. }
  28. return res;
  29. }
  1. int numberOfBoomerangs(vector<pair<int, int>>& points) {
  2. int booms = 0;
  3. for (auto &p : points) {
  4. unordered_map<double, int> ctr(points.size());
  5. for (auto &q : points)
  6. booms += 2 * ctr[hypot(p.first - q.first, p.second - q.second)]++;
  7. }
  8. return booms;
  9. }

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.

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

  1. Leetcode447.Number of Boomerangs回旋镖的数量

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

  2. [Swift]LeetCode447. 回旋镖的数量 | 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. [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: Number of Boomerangs

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

  6. 34. leetcode 447. Number of Boomerangs

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

  7. 447. Number of Boomerangs

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

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

  9. LeetCode——Number of Boomerangs

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

随机推荐

  1. 一个简单的WeakList的实现

    有的时候,我们会使用到WeakList,它和传统的List不同的是,保存的是对象的弱应用,在WeakList中存储的对象会被GC回收,在一些和UI相关的编程的地方会用到它(弱事件,窗体的通知订阅等). ...

  2. WebLogic Server 多租户资源管理(resource consume manager)

    WebLogic Server基于分区管理heap Size,CPU利用率等,具体的设置如下, 首先需要在setDomainEnv.cmd文件中java_option中加入如下字段 -XX:+Unlo ...

  3. WebHelper-SessionHelper、CookieHelper、CacheHelper、Tree

    ylbtech-Unitity: cs-WebHelper-SessionHelper.CookieHelper.CacheHelper.Tree SessionHelper.cs CookieHel ...

  4. Gitlab系列九之取消用户注册页面和删除用户

    一.取消用户注册页面 Admin Area--->Settings--->Sign-up enabled(取消前面的勾)---save 二.删除用户 Users----Destroy(点他 ...

  5. FL2440 ubifs文件系统烧录遇到的问题——内核分区的重要性

    之前用的文件系统是initramfs的,这种文件系统是编译进内核里的,而开机之后内核是写在内存中的,所以每次掉电之后写进文件系统中的东西都会丢失.所以决定换成ubifs的文件系统.这种文件系统是跟内核 ...

  6. 查看MySQL数据库大小

    查看MySQL数据库大小 1.首先进入information_schema 数据库(存放了其他的数据库的信息) ? 1 2 mysql> use information_schema; Data ...

  7. Hadoop之Hbase详解

    1.什么是Hbase HBASE是一个高可靠性.高性能.面向列.可伸缩的分布式存储系统, hbase是列式的分布式数据库 1.2.HBASE优势: 1)线性扩展,随着数据量增多可以通过节点扩展进行支撑 ...

  8. LINUX 和WINDOWS下的自动登录小脚本

    每天上班第一件事,就是连接公司LAB里面的机器,但首先要过一个防火墙,每次输用户名密码是很累人的事, 以下是两个脚本,可以放在启动项中,开机便自动登录 WINDOWS: @echo off ipcon ...

  9. 被动信息收集1——DNS基础 + DNS解析工具 NSLOOKUP使用

    被动信息收集 特点: 基于公开渠道 与目标不直接接触 避免留下一切痕迹 标准參考--OSINT: 美国军方 北大西洋公约组织 名词解释 DNS--Domain Name System 域名系统 因特网 ...

  10. Android Exception 15(关于使用RecyclerView的异常)

    04-07 16:32:32.815: E/AndroidRuntime(16173): FATAL EXCEPTION: main 04-07 16:32:32.815: E/AndroidRunt ...