447. Number of Boomerangs
来源:互联网 发布:火箭发射 模拟软件 编辑:程序博客网 时间:2024/06/07 11:11
题目:
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:2Explanation:The two boomerangs are [[1,0],[0,0],[2,0]] and [[1,0],[2,0],[0,0]]思路:
本题思路很简单,实现两个函数,一个是求距离的函数
sumq(pair<int,int> p1,pair<int,int> p2)一个是求个数的函数
sumn(map<long,int> res)
通过两个嵌套for循环来实现遍历
代码:
class Solution {public: int numberOfBoomerangs(vector<pair<int, int>>& points) { map<long,int> res; int sum = 0; for(int i =0;i<=points.size()-1;i++) { for(int j =0;j<=points.size()-1;j++) { if(j!=i) res[(sumq(points[i],points[j]))]++; } sum+=sumn(res); res.clear(); } return sum; }private: long sumq(pair<int,int> p1,pair<int,int> p2){ long distance = (p1.first-p2.first)*(p1.first-p2.first)+(p1.second-p2.second)*(p1.second-p2.second); return distance; } int sumn(map<long,int> res){ map<long,int>::iterator itr = res.begin(); int count =0; while(itr!=res.end()) { if(itr->second>1) count+=itr->second*(itr->second-1); itr++; } return count; } };
阅读全文
1 0
- 447. Number of Boomerangs
- 447. Number of Boomerangs
- 447. Number of Boomerangs
- 447. Number of Boomerangs
- 447. Number of Boomerangs
- 447. Number of Boomerangs
- 447. Number of Boomerangs
- 447. Number of Boomerangs
- 447. Number of Boomerangs
- 447. Number of Boomerangs*
- 447. Number of Boomerangs
- 447. Number of Boomerangs
- 447. Number of Boomerangs
- 447. Number of Boomerangs
- 447. Number of Boomerangs
- 447. Number of Boomerangs
- 447. Number of Boomerangs
- 447. Number of Boomerangs
- 学习ReactiveCocoa教程 (整理的资料)
- 在linux下将自己的shell代码当做系统命令一样使用
- 用grunt搭建自动化的web前端开发环境(二)配置grunt 【转载自博客园北执】
- 模态对话框适用场景及注意事项
- git学习:
- 447. Number of Boomerangs
- linux 输入输出重定向
- android 视频播放器 TextureView , MediaPlayer , SurfaceView 的坑
- 用JS判断两个数字的大小
- 数字营销产业链介绍
- Unity入门操作_向量_002
- poj1038 Bugs Integrated, Inc.(3进制状压dp)
- 中序遍历
- bzoj1187: [HNOI2007]神奇游乐园