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, choose 2 (with permutation) from it, to form a boomerang with point i.

[use long appropriately for dx, dy and key; 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的更多相关文章

  1. Leetcode447.Number of Boomerangs回旋镖的数量

    给定平面上 n 对不同的点,"回旋镖" 是由点表示的元组 (i, j, k) ,其中 i 和 j 之间的距离和 i 和 k 之间的距离相等(需要考虑元组的顺序). 找到所有回旋镖的 ...

  2. [Swift]LeetCode447. 回旋镖的数量 | Number of Boomerangs

    Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of po ...

  3. [LeetCode] Number of Boomerangs 回旋镖的数量

    Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of po ...

  4. [LeetCode]447 Number of Boomerangs

    Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of po ...

  5. Leetcode: Number of Boomerangs

    Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of po ...

  6. 34. leetcode 447. Number of Boomerangs

    Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of po ...

  7. 447. Number of Boomerangs

    Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of po ...

  8. [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 ...

  9. LeetCode——Number of Boomerangs

    LeetCode--Number of Boomerangs Question Given n points in the plane that are all pairwise distinct, ...

随机推荐

  1. Scala零基础教学【61-80】

    第61讲:Scala中隐式参数与隐式转换的联合使用实战详解及其在Spark中的应用源码解析 第62讲:Scala中上下文界定内幕中的隐式参数与隐式参数的实战详解及其在Spark中的应用源码解析 /** ...

  2. 【java】File的使用:将字符串写出到本地文件,大小0kb的原因

    实现方法: 暂时写一种方法,将字符串写出到本地文件,以后可以补充更多种方法: public static void main(String[] args) { /** * ============== ...

  3. webpack配置构建环境问题汇总

    环境:node 6.9.5  npm 3.10.10 问题一:Module build failed: TypeError: Path must be a string. Received undef ...

  4. fl2440字符设备led驱动

    首先要明白字符设备驱动注册的基本流程 当我们调用insomd命令加载驱动后,驱动程序从module_init函数开始执行:硬件初始化 -> 申请主次设备号 -> 定义fops(file_o ...

  5. Solr6 Suggest(智能提示)

    1.介绍 Solr从1.4开始便提供了检查建议,检索建议目前是各大搜索的标配应用,主要作用是避免用户输入错误的搜索词,同时将用户引导到相应的关键词搜索上.通常,我们将其称为搜索联想. 其效果如图所示. ...

  6. C# SendMail 发送邮件

    最近因为用的发送邮件的地方,就查询了资料,总结以下几个方法 1.利用新浪邮箱发送 2.利用公司邮箱发送 3.利用CDO发送,这种方式要引用Interop.ADODB.dll(http://www.no ...

  7. [Android]一些设计细节

    1. 图标 图标分为:Launcher 图标(程序图标),ActionBar 图标(菜单图标),Contextual 图标(嵌入的小图标)以及Notification 图标(通知栏图标).每种图标都有 ...

  8. knowledgeroot

    knowledgeroot 示例站点 www.globaladmin.cn Knowledgeroot可用于文档管理,知识库管理. 1.基于php开发,支持linux ,windows.2.支持mys ...

  9. spock+maven+junitReport接口测试框架

    1.POM 文件: <?xml version="1.0" encoding="UTF-8"?><project xmlns="ht ...

  10. mysqli 预处理语句

    预处理语句用于执行多个相同的 SQL 语句,并且执行效率更高. <?php // 设置编码格式 header('content-type:text/html;charset=utf-8'); / ...