Number of Boomerangs问题及解法
2017-04-22 09:42
393 查看
问题描述:
Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of points
the distance between
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).
示例:
问题分析:
本题要求两个点到某一点的距离相同,根据题意,我们可以固定一个点i,遍历i之后的点j,计算i与j的距离,存储到map中,并记录好每个距离的出现的次数。每次遍历完成,根据排列组合方式求解即可。
过程详见代码:
class Solution {
public:
int numberOfBoomerangs(vector<pair<int, int>>& points) {
int res = 0;
for (int i = 0; i < points.size(); ++i) {
unordered_map<long, int> group(points.size());// c++新特性
for (int j = 0; j < points.size(); ++j) {
if (j == i) continue;
int dy = points[i].second - points[j].second;
int dx = points[i].first - points[j].first;
int key = dy * dy;
key += dx * dx;
++group[key];
}
for (auto& p : group) {
if (p.second > 1) {
res += p.second * (p.second - 1);
}
}
}
return res;
}
};
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
iand
jequals
the distance between
iand
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).
示例:
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]]
问题分析:
本题要求两个点到某一点的距离相同,根据题意,我们可以固定一个点i,遍历i之后的点j,计算i与j的距离,存储到map中,并记录好每个距离的出现的次数。每次遍历完成,根据排列组合方式求解即可。
过程详见代码:
class Solution {
public:
int numberOfBoomerangs(vector<pair<int, int>>& points) {
int res = 0;
for (int i = 0; i < points.size(); ++i) {
unordered_map<long, int> group(points.size());// c++新特性
for (int j = 0; j < points.size(); ++j) {
if (j == i) continue;
int dy = points[i].second - points[j].second;
int dx = points[i].first - points[j].first;
int key = dy * dy;
key += dx * dx;
++group[key];
}
for (auto& p : group) {
if (p.second > 1) {
res += p.second * (p.second - 1);
}
}
}
return res;
}
};
相关文章推荐
- Minimum Number of Arrows to Burst Balloons问题及解法
- Maven出现以下问题:The Maven Integration requires that Eclipse be running in a JDK, because a number of Maven core plugins are using ja
- java call oracle procudure setBoolean问题PLS-00306: wrong number or types of arguments in call to
- redis拒绝服务、报max number of clients reached问题
- [LeetCode] Number of Boomerangs 回旋镖的数量
- Number of Boomerangs
- projecteuler网站第三题解法:Get the largest prime factor of a number
- LeetCode Number of Boomerangs
- ibatis 中遇到的 Parameter index out of range (13 > number of parameters, which is 12).问题
- jxl单元格格式丢失问题解决(Maximum number of format records exceeded)
- [LeetCode]447 Number of Boomerangs
- jxl下拉长度的问题,Validation list exceeds maximum number of characters - truncating
- BIRT出现java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0)的问题
- Leetcode中的组合排列问题:Permutations,Combinations,Letter Combinations of a Phone Number
- mysql 主从复制问题'the master returned an invalid number of fields for SHOW SLAVE HOSTS'
- 【内存管理-问题实例】为什么执行了numberOfRowsInSection方法后,dataArray的retainCount就少了一个? 谁把它 release了?
- LeetCode Letter Combinations of a Phone Number 经典组合问题
- LeetCode Single Number I & II 都符合两个问题额外要求的 通用解法 与 思考过程
- number-of-boomerangs
- Android “64k”的问题以及解决方案 Error:The number of method references in a .dex file cannot exceed 64K.