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.

具体实现

#include <iostream>
#include <vector>
#include <map> using namespace std; class Solution {
public:
int numberOfBoomerangs(vector<pair<int, int>>& points) {
int res = 0; for (int i = 0; i < points.size(); i++) {
map<int, int> dis_map; for (int j = 0; j < points.size(); j++) {
if (i != j) {
int dis = distance(points[i], points[j]);
dis_map[dis]++;
}
} for (map<int, int>::iterator it = dis_map.begin(); it != dis_map.end(); it++) {
res += (it->second * (it->second - 1) / 2) * 2;
}
} return res;
}
int distance(pair<int, int>& a, pair<int, int>& b) {
int x = (a.first - b.first) * (a.first - b.first);
int y = (a.second - b.second) * (a.second - b.second);
return x + y;
}
};

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. pandas 读取文件

    import pandas as pd import matplotlib.pyplot as plt data = pd.read_csv('G:timeCompare.txt', sep=' ', ...

  2. poj3414

    Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13545   Accepted: 5717   Special J ...

  3. HTML中条件注释的高级应用

    在页面头部加入 <!--[if lt IE 9]><html class="ie"><![endif]--> 可简单CSS Hack,IE6.I ...

  4. delphi中 ExecSQL 与 open

    对于不用返回结果集的要用execsql反之则用open;insert ,update,delete就要用到execsql;select就要用open 说得对,例子:with query1 do clo ...

  5. Powershell Get-ChildItem 筛选文件,文件处理

    使用Where-Object也可以根据其它属性来过滤. Dir | Where-Object { $_.CreationTime -gt [datetime]::Parse("May 12, ...

  6. soft deletion Google SRE 保障数据完整性的手段

    w http://www.infoq.com/cn/articles/GoogleSRE-BookChapter26 Google SRE 保障数据完整性的手段 就像我们假设Google 的底层系统经 ...

  7. express, mocha, supertest,istanbul

    引子 有群友问到Express怎么做 单元测试/覆盖率测试,这是上篇所遗漏的,特此补上 Express Web测试 做 Express Web 测试首先要面对的问题是在哪端进行测试: 客户端的请求响应 ...

  8. 接口测试工具 — jmeter(参数化)

    1.用户定义的变量 添加一个用户定义的变量 添加变量值 2.函数生成器 函数生成 随机数生成 取当前时间 3.从文件中读取 1)新建一个TXT文档,录入数据 2)读取文件 3)使用数据,用 ${pho ...

  9. html当前文档的状态

    <script type="text/javascript"> document.onreadystatechange = loadingChange;//当页面加载状 ...

  10. Linux环境安装配置maven

     按照下面命令执行即可 1.下载apache-maven-3.5.3-bin.tar.gz 并上传到服务器上 提取地址:https://pan.baidu.com/s/11nxZp84lmonRBCR ...