447. Number of Boomerangs
Problem:
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]]
思路:
Solution (C++):
int numberOfBoomerangs(vector<vector<int>>& points) {
if (points.empty() || points[0].empty()) return 0;
int m = points.size(), count = 0, res = 0;
vector<int> dis(m, 0);
for (int i = 0; i < m; ++i) {
for (int j = 0; j < m; ++j) {
if (i == j) dis[j] = 0;
dis[j] = distance(points[i], points[j]);
}
sort(dis.begin(), dis.end());
for (int k = 0; k < m-1; ++k) {
while (k < m-1 && dis[k] == dis[k+1]) { count++; k++; }
if (count >= 2) res += An2(count);
count = 1;
}
}
return res;
}
int distance(vector<int> v1, vector<int> v2) {
return pow(v2[0]-v1[0], 2) + pow(v2[1]-v1[1], 2);
}
int An2(int n) {
return n * (n-1);
}
性能:
Runtime: 1416 ms Memory Usage: 161.7 MB
思路:
Solution (C++):
性能:
Runtime: ms Memory Usage: MB
相关链接如下:
知乎:littledy
GitHub主页:https://github.com/littledy
github.io:https://littledy.github.io/
欢迎关注个人微信公众号:小邓杂谈,扫描下方二维码即可
![](https://www.cnblogs.com/images/cnblogs_com/dysjtu1995/1468066/t_qrcode_for_gh_547d3edeac81_258.jpg)
作者:littledy
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。