LeetCode447. Number of Boomerangs
Description
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]]
my program
思路:创建
points.size()*points.size()
的数组distance
,存放每个点到各个点的距离的平方(距离的话需要开方,产生了浮点数,这里避免了),对数组的每一行元素进行排序,找出每行中某个元素相同的数目n
,然后计数count
累加(1+2+...+n)
的和
class Solution {
public:
int sum(int n) //求1~n的和
{
int res = 0;
while(n)
{
res += n--;
}
return res;
}
int numberOfBoomerangs(vector<pair<int, int>>& points) {
int count = 0;
if(points.size() <= 2) return count;
vector<vector<long long> > distance;
//points.size()*points.size()的数组
//存放每个点到各个点的距离的平方(距离的话需要开方,产生了浮点数,这里避免了)
for(int i = 0; i<points.size(); i++) //初始化数组
{
vector<long long>temp(points.size(),0);
distance.push_back(temp);
}
for(int i = 0;i<points.size(); i++) //填充数组(计算距离平方填充)
{
for(int j = 0; j<points.size(); j++)
{
long long x = points[i].first - points[j].first;
long long y = points[i].second - points[j].second;
distance[i][j] = x*x + y*y;
}
}
for(int i = 0;i<points.size(); i++)
{
sort(distance[i].begin(),distance[i].end());
int j = 0;
while(j<points.size()-1)
{
int n = 0;
//每行中某个元素相同的数目
while(j<points.size()-1 && distance[i][j] == distance[i][j+1])
{
n++;
j++;
}
if( n != 0) count += sum(n)*2;
j++;
}
}
return count;
}
};
Submission Details
31 / 31 test cases passed.
Status: Accepted
Runtime: 175 ms
Your runtime beats 92.00% of cpp submissions.
other methods
For each point
i
,map<distance d, count of all points at distance d from i>
.
Given that count, choose2
(with permutation) from it, to form a boomerang with pointi
.
[use long appropriately fordx
,dy
andkey
; though not required for the given test cases]
Time Complexity:O(n^2)
int numberOfBoomerangs(vector<pair<int, int>>& points) {
int res = 0;
// iterate over all the points
for (int i = 0; i < points.size(); ++i) {
unordered_map<long, int> group(points.size());
// iterate over all points other than points[i]
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;
// compute squared euclidean distance from points[i]
int key = dy * dy;
key += dx * dx;
// accumulate # of such "j"s that are "key" distance from "i"
++group[key];
}
for (auto& p : group) {
if (p.second > 1) {
/*
* for all the groups of points,
* number of ways to select 2 from n =
* nP2 = n!/(n - 2)! = n * (n - 1)
*/
res += p.second * (p.second - 1);
}
}
}
return res;
}
int numberOfBoomerangs(vector<pair<int, int>>& points) {
int booms = 0;
for (auto &p : points) {
unordered_map<double, int> ctr(points.size());
for (auto &q : points)
booms += 2 * ctr[hypot(p.first - q.first, p.second - q.second)]++;
}
return booms;
}
Try each point as the “axis” of the boomerang, i.e., the “i” part of the triple. Group its distances to all other points by distance, counting the boomerangs as we go. No need to avoid q == p, as it’ll be alone in the distance == 0 group and thus won’t influence the outcome.
Submitted five times, accepted in 1059, 1022, 1102, 1026 and 1052 ms, average is 1052.2 ms. The initial capacity for ctr isn’t necessary, just helps make it fast. Without it, I got accepted in 1542, 1309, 1302, 1306 and 1338 ms.
LeetCode447. Number of Boomerangs的更多相关文章
- Leetcode447.Number of Boomerangs回旋镖的数量
给定平面上 n 对不同的点,"回旋镖" 是由点表示的元组 (i, j, k) ,其中 i 和 j 之间的距离和 i 和 k 之间的距离相等(需要考虑元组的顺序). 找到所有回旋镖的 ...
- [Swift]LeetCode447. 回旋镖的数量 | Number of Boomerangs
Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of po ...
- [LeetCode] 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: 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——Number of Boomerangs
LeetCode--Number of Boomerangs Question Given n points in the plane that are all pairwise distinct, ...
随机推荐
- How to determine what causes a particular wait type
By: Paul Randal Posted on: March 18, 2014 6:55 pm [Edit 2016: Check out my new resource – a comp ...
- 使用Jenkins部署Spring Boot
原文:http://www.cnblogs.com/ityouknow/p/7899349.html jenkins是devops神器,本篇文章介绍如何安装和使用jenkins部署Spring Boo ...
- Array.apply 方法的使用
Array.apply(null, {length: 5}) length为特殊字段,意思是生成一个长度为5的数组,由于没赋值,所以都是undefined; 如果要赋值,可以这样 console.lo ...
- 表格中的IE BUG
在表格应用了跨列单元格后,在IE6/7下当跨列单元格中的元素长度超过其跨列单元格中第一个单元格的宽度时会产生换行,如下所示: 解决方法: 1. 设置 table 的 'table-layout' 特性 ...
- 【自动部署】服务器自动化操作 RunDeck
RunDeck 是用 Java/Grails 写的开源工具,帮助用户在数据中心或者云环境中自动化各种操作和流程.通过命令行或者web界面,用户可以对任意数量的服务器进行操作,大大降低了对服务器自动化的 ...
- vlan 介绍
简介 在Linux中安装了802.1Q标签VLAN功能.VLAN是虚拟分配以太网的功能. 使用VLAN ID从物理上将一个以太网分割开.在VLAN环境下,具有相同VLAN ID 就可以相互通 ...
- 三位一体的漏洞分析方法-web应用安全测试方法
本文转自乌云知识库 0x00 前言 节选自: http://www.owasp.org.cn/OWASP_Conference/owasp-20140924/02OWASPWeb20140915.pd ...
- angular - 如何运行在起来 - 使用nginx
nginx下载地址,使用的是标准版的: 点击下载nginx nginx下载完后,解压 dist文件夹下面所有angular文件放入html文件夹中. 最后命令行cd到当前nginx.exe目录,启动命 ...
- Struts2数据类型转换之批量数据转换
前面我们实现了从字符串到User对象的转换.如果表单中有多个User数据,我们可以批量转换. 我们把input.jsp修改为: <h1>使用分号隔开username password< ...
- redux-actions
其作用都是用来简化action.reducer. 1.安装 npm install --save redux-actions // 或 yarn add redux-actions 2.使用 crea ...