要求

  • 给出平面上n个点,寻找存在多少点构成的三元组(i j k),使得 i j 两点的距离等于 i k 两点的距离
  • n 最多为500,所有点坐标范围在[-10000, 10000]之间

示例

  • [[0,0],[1,0],[2,0]]
  • 2

思路

  • 对于每个点i,遍历其余点到i的距离(时间n2,空间n)
  • 查找表记录其他点到 i 的<距离,频次>
  • 对于每个距离,满足条件的三元组个数为:频次 x (频次-1)
  • 求距离时用平方,防止浮点数误差
  • 最长距离,2000^2+2000^2,32位整型不越界

实现

  • 用pair存储点坐标
 1 #include <iostream>
2 #include <vector>
3 #include <unordered_map>
4 #include <cassert>
5 #include <stdexcept>
6 #include <typeinfo>
7
8 using namespace std;
9
10 /// Using Hash Map
11 /// Time Complexity: O(n^2)
12 /// Space Complexity: O(n)
13 class Solution {
14 public:
15 int numberOfBoomerangs(vector<pair<int, int>>& points) {
16
17 int res = 0;
18 for( int i = 0 ; i < points.size() ; i ++ ){
19
20 // record中存储 点i 到所有其他点的距离出现的频次
21 unordered_map<int, int> record;
22 for(int j = 0 ; j < points.size() ; j ++)
23 if(j != i)
24 // 计算距离时不进行开根运算, 以保证精度
25 record[dis(points[i], points[j])] += 1;
26
27 for(unordered_map<int, int>::iterator iter = record.begin() ; iter != record.end() ; iter ++)
28 res += (iter->second) * (iter->second - 1);
29 }
30 return res;
31 }
32
33 private:
34 int dis(const pair<int,int> &pa, const pair<int,int> &pb){
35 return (pa.first - pb.first) * (pa.first - pb.first) +
36 (pa.second - pb.second) * (pa.second - pb.second);
37 }
38 };
39
40
41 int main() {
42 //int a = 1;
43 vector<pair<int,int>> vec;
44 vec.push_back(make_pair(0, 0));
45 vec.push_back(make_pair(1, 0));
46 vec.push_back(make_pair(2, 0));
47
48 cout << Solution().numberOfBoomerangs(vec) << endl;
49 return 0;
50 }
  • 用vector存储点坐标
 1 #include <iostream>
2 #include <vector>
3 #include <unordered_map>
4 #include <cassert>
5 #include <stdexcept>
6 #include <typeinfo>
7
8 using namespace std;
9
10 /// Using Hash Map
11 /// Time Complexity: O(n^2)
12 /// Space Complexity: O(n)
13 class Solution {
14 public:
15 int numberOfBoomerangs(vector<vector<int>>& points) {
16
17 int res = 0;
18 for( int i = 0 ; i < points.size() ; i ++ ){
19
20 // record中存储 点i 到所有其他点的距离出现的频次
21 unordered_map<int, int> record;
22 for(int j = 0 ; j < points.size() ; j ++)
23 if(j != i)
24 // 计算距离时不进行开根运算, 以保证精度
25 record[dis(points[i], points[j])] += 1;
26
27 for(unordered_map<int, int>::iterator iter = record.begin() ; iter != record.end() ; iter ++)
28 res += (iter->second) * (iter->second - 1);
29 }
30 return res;
31 }
32
33 private:
34 int dis(const vector<int> &pa, const vector<int> &pb){
35 return (pa[0] - pb[0]) * (pa[0] - pb[0]) +
36 (pa[1] - pb[1]) * (pa[1] - pb[1]);
37 }
38 };
39
40 int main() {
41 vector<vector<int>> vec;
42 vector<int> vn;
43
44 vn.push_back(0);
45 vn.push_back(0);
46 vec.push_back(vn);
47 vn.clear();
48
49 vn.push_back(1);
50 vn.push_back(0);
51 vec.push_back(vn);
52 vn.clear();
53
54 vn.push_back(2);
55 vn.push_back(0);
56 vec.push_back(vn);
57 vn.clear();
58
59 cout << Solution().numberOfBoomerangs(vec) << endl;
60 return 0;
61 }

相关

  • 149 Max Points on a Line

[刷题] 447 Number of Boomerangs的更多相关文章

  1. 447. Number of Boomerangs 回力镖数组的数量

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

  2. [LeetCode]447 Number of Boomerangs

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

  3. 34. leetcode 447. Number of Boomerangs

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

  4. 447. Number of Boomerangs

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

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

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

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

  8. 447 Number of Boomerangs 回旋镖的数量

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

  9. 【leetcode】447. Number of Boomerangs

    题目如下: 解题思路:我首先用来时间复杂度是O(n^3)的解法,会判定为超时:后来尝试O(n^2)的解法,可以被AC.对于任意一个点,我们都可以计算出它与其余点的距离,使用一个字典保存每个距离的点的数 ...

随机推荐

  1. Java学习笔记--文件IO

    简介 对于任何程序设计语言,输入和输出(Input\Output)都是系统非常核心的功能,程序运行需要数据,而数据的获取往往需要跟外部系统进行通信,外部系统可能是文件.数据库.其他程序.网络.IO设备 ...

  2. CentOS 7.6部署Vue + SrpingBoot + MySQL单体项目

    对于独立的项目(前端.后台单体服务.数据库),部署到新服务器上时,常常需要繁琐的配置与环境安装,这里介绍Centos 7.6下如何搭建基于Docker的环境,以及如何使用docker部署一套Vue + ...

  3. day-5 xctf-when_did_you_born

    xctf-when_did_you_born 题目传送门:https://adworld.xctf.org.cn/task/answer?type=pwn&number=2&grade ...

  4. 【笔记】《Redis设计与实现》chapter11 AOF持久化

    11.1 AOF持久化的实现 命令追加 当AOF持久化处于开启状态时,服务器执行完一个写命令之后,会以协议格式将被执行的写明了追加到服务器状态的aof_buf缓冲区 struct redisServe ...

  5. JFX11+IDEA跨平台打包发布的完美解决办法

    1 概述 IDEA2020.1的文档中提到只有JFX8的工程才支持打成jar包,并且,如果直接使用Build Artifacts的话,会如下提示: IDEA文档有提到这个的解决办法,是使用一些第三方工 ...

  6. 不可不知的CSS小技巧

    一.表单部分 1.禁止textarea文本域的缩放 resize:none; 2.去除初始化textarea下拉条 overflow:auto; 3.如何让表单中的选项按钮,点击文字也能选中? < ...

  7. LA3403 天平难题

    题意:      给出房间的宽度r和每个吊坠的重量wi,设计一个尽量宽但宽度不能超过房间宽度的天平,挂着所有挂坠,每个天平的一段要么挂这一个吊坠,要么挂着另一个天平,每个天平的总长度是1,细节我给出题 ...

  8. UVA11021麻球繁衍

    题意:      有K只麻球,每只生存一天就会死亡,每只麻球在死之前有可能生下一些麻球,生i个麻球的概率是pi,问m天后所有的麻球都死亡的概率是多少? 思路:       涉及到全概率公式,因为麻球的 ...

  9. 【python】Leetcode每日一题-扰乱字符串

    [python]Leetcode每日一题-扰乱字符串 [题目描述] 使用下面描述的算法可以扰乱字符串 s 得到字符串 t : 如果字符串的长度为 1 ,算法停止 如果字符串的长度 > 1 ,执行 ...

  10. FreeSql之Expression表达式拼接参数扩展

    在FreeSql源码中Expression表达式拼接默认最多支持到5个泛型参数,当我们使用表关联比较多的时候,就需要进行扩展. 新建一个类,将命名空间改为System.Linq.Expressions ...