[刷题] 447 Number of Boomerangs
要求
- 给出平面上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的更多相关文章
- 447. Number of Boomerangs 回力镖数组的数量
[抄题]: Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple ...
- [LeetCode]447 Number of Boomerangs
Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of po ...
- 34. leetcode 447. Number of Boomerangs
Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of po ...
- 447. Number of Boomerangs
Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of po ...
- [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 ...
- LeetCode 447. Number of Boomerangs (回力标的数量)
Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of po ...
- 【LeetCode】447. Number of Boomerangs 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 [LeetCode] 题目地址:https:/ ...
- 447 Number of Boomerangs 回旋镖的数量
给定平面上 n 对不同的点,“回旋镖” 是由点表示的元组 (i, j, k) ,其中 i 和 j 之间的距离和 i 和 k 之间的距离相等(需要考虑元组的顺序).找到所有回旋镖的数量.你可以假设 n ...
- 【leetcode】447. Number of Boomerangs
题目如下: 解题思路:我首先用来时间复杂度是O(n^3)的解法,会判定为超时:后来尝试O(n^2)的解法,可以被AC.对于任意一个点,我们都可以计算出它与其余点的距离,使用一个字典保存每个距离的点的数 ...
随机推荐
- SpingCloud Alibaba实战(1:微服务与SpringCloud Alibaba)
1.什么是微服务? 微服务可谓是这几年比较热门的技术,从2017开始逐渐爆火,逐渐大大小小的公司纷纷将微服务技术引入并在实际业务中落地. 微服务的概念最早是在2014年由Martin Fowler和J ...
- [素数判断]P1125 笨小猴
笨小猴 题目描述 笨小猴的词汇量很小,所以每次做英语选择题的时候都很头疼.但是他找到了一种方法,经试验证明,用这种方法去选择选项的时候选对的几率非常大! 这种方法的具体描述如下:假设maxn是单词中出 ...
- --系统编程-网络-tcp客户端服务器编程模型、socket、htons、inet_ntop等各API详解、使用telnet测试基本服务器功能
PART1 基础知识 1. 字节序 网络字节序是大端字节序(低地址存放更高位的字节), 所以,对于字节序为小端的机器需要收发网络数据的场景,要对这些数据进行字节序转换. 字节序转换函数,常用的有四个: ...
- java面试-JVM常用的基本配置参数有哪些?
1.-Xms 初始大小内存,默认为物理内存 1/64,等价于 -XX:InitialHeapSize 2.-Xmx 最大分配内存,默认为物理内存的 1/4,等价于 -XX:MaxHeapSize 3. ...
- C#类的一些基础知识(静态方法可以不用实例化调用)
将类成员函数声明为public static无需实例化即可调用类成员函数 using System; namespace ConsoleApp { class Program { static voi ...
- 【Git基本命令】
[基本指令] git init :使目标文件夹变成一个仓库 git add <文件名,含后缀> : 告诉git我要添加文件了 git commit -m "<提交说明> ...
- Git版本控制之-创建配置本地git仓库
查看全局配置:code .gitconfig [code 就代表的用vscode 打开gitconfig 文件,如果是 sublime 就是 subl ][如果打开失败说明环境变量没有配置] [只有配 ...
- 现代操作系统原书第3版.mobi
电子书资源:现代操作系统原书第3版 书籍简介 本书是操作系统领域的经典之作,与第2版相比,增加了关于Linux.Windows Vista和Symbian操作系统的详细介绍.书中集中讨论了操作系统 ...
- C - 抽屉 POJ - 3370 (容斥原理)
Every year there is the same problem at Halloween: Each neighbour is only willing to give a certain ...
- ubuntu 缺少动态依赖库
起因 困扰我好久的一个报错,终于解决了 之前我一直以为是 python代码的问题,以为是模块相互调引起的报错,忽略了最后一行这个错误 OSError: libGCBase_gcc421_v3_0.so ...