LeetCode——Number of Boomerangs

Question

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

解题思路

注意这道题只需要求有多少对,没有具体要求是哪些,所以可以只记录点之间的距离。可以考虑用一个hash table记录每一个点到其他所有点的距离,如果相同距离数目大于等于2, 那么必有满足要求的组合。就是最后的时候注意如何求这样的组合。假如距离为6的点对是a, 那么这样的对数就是: (a * (a - 1) / 2) * 2.

具体实现

  1. #include <iostream>
  2. #include <vector>
  3. #include <map>
  4. using namespace std;
  5. class Solution {
  6. public:
  7. int numberOfBoomerangs(vector<pair<int, int>>& points) {
  8. int res = 0;
  9. for (int i = 0; i < points.size(); i++) {
  10. map<int, int> dis_map;
  11. for (int j = 0; j < points.size(); j++) {
  12. if (i != j) {
  13. int dis = distance(points[i], points[j]);
  14. dis_map[dis]++;
  15. }
  16. }
  17. for (map<int, int>::iterator it = dis_map.begin(); it != dis_map.end(); it++) {
  18. res += (it->second * (it->second - 1) / 2) * 2;
  19. }
  20. }
  21. return res;
  22. }
  23. int distance(pair<int, int>& a, pair<int, int>& b) {
  24. int x = (a.first - b.first) * (a.first - b.first);
  25. int y = (a.second - b.second) * (a.second - b.second);
  26. return x + y;
  27. }
  28. };

LeetCode——Number of Boomerangs的更多相关文章

  1. [LeetCode] Number of Boomerangs 回旋镖的数量

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

  2. Leetcode: Number of Boomerangs

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

  3. Python3解leetcode Number of Boomerangs

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

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

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

  6. [LeetCode]447 Number of Boomerangs

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

  7. 34. leetcode 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. C#LeetCode刷题之#447-回旋镖的数量(Number of Boomerangs)

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

随机推荐

  1. JZOJ.5285【NOIP2017模拟8.16】排序

    Description

  2. List 接口常用子类及其特点

    List 常用子类: - Vector: 内部是数组数据结构,是同步的. 增删, 查询都很慢 - ArrayList: 内部是数组数据结构,是不同步的,替代了 Vector,不同步的效率较高. 特点: ...

  3. 解决ajax post json数据 后端无法收到的问题

    如图,想把input框中的文字以json格式post出去,结果后端收不到 使用wireshark抓包,根本没有抓到发往服务器的包,说明错误在前端. 后来发现ajax post json数据的时候key ...

  4. 我的Android进阶之旅------>Android Studio 快捷键整理分享

    正式转战Android Studio了,首先把Android Studio的快捷键摘录下来,以备后用. (官网的快捷键列表如下  https://developer.android.com/studi ...

  5. android应用程序优化之布局优化

    在我们开发APP时不仅要在代码实现上.做到对App的优化,而在我们的界面布局也有很多要优化的地方,假设布局写的非常low的话,系统载入布局的速度会十分的慢,使得用户的体验非常的不好.这篇文章主要是从我 ...

  6. 目标检测之R-FCN

    R-FCN:Object Detection via Region-based Fully Convolutional Networks R-FCN的网络结构 一个Base的convolutional ...

  7. tomcat 日志目录 介绍

    [root@mysql tomcat]# ll 总用量 drwxr-x---. root root 11月 : bin -rw-r-----. root root 11月 : BUILDING.txt ...

  8. 如何使用django中的cookie和session?

    1.Cookie 介绍 Cookie是由服务器端生成,发送给User-Agent(一般是浏览器),浏览器会将Cookie的key/value保存到某个目录下的文本文件内,下次请求同一网站时就发送该Co ...

  9. 转:[NHibernate]视图处理

    转自:http://www.cnblogs.com/wolf-sun/p/4082899.html 目录 写在前面 文档与系列文章 视图 一个例子 总结 写在前面 前面的文章主要讲了对物理数据表的操作 ...

  10. 转:MVC遇上bootstrap后的ajax表单验证

    使用bootstrap后他由他自带的样式has-error,想要使用它就会比较麻烦,往常使用jqueyr.validate的话只有使用他自己的样式了,而且有模型在使用模型验证更方便点.怎么解决呢? 当 ...