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. Android中的动态字符串的处理

    1.效果显示 2. MainAcitivity.java package com.example.app2; import android.support.v7.app.AppCompatActivi ...

  2. IntelliJ IDEA控制台输出中文乱码问题解决

    如果还不行,那么再极端的设置,在IDEA启动的时候强制设置为UTF-8: 打开增加-Dfile.encoding=UTF-8,重启Intellij IDEA 再或者直接在项目运行的时候加入UTF-8的 ...

  3. Oracle RAC 环境下的 v$log v$logfile

    通常情况下,在Oracle RAC 环境中,v$视图可查询到你所连接实例的相关信息,而gv$视图则包含所有实例的信息.然而在RAC环境中,当我们查询v$log视图时说按照常理的话,v$log视图应当看 ...

  4. 解决Visual Studio 2013 XAML设计器异常

    今天使用Visual Studio 2013打开一个windows 应用商店程序的时候,发现出现如下异常:    at System.Windows.Input.Cursor.LoadFromFile ...

  5. 直接拿来用!最火的iOS开源项目(三)

    相比Android,GitHub上的iOS开源项目更可谓是姹紫嫣红.尽管效果各异,但究其根源,却都是因为开发者本身对于某种效果的需求以及热爱.在“直接拿来用!最火的iOS开源项目”系列文章(一).(二 ...

  6. ext js layout and tree

    数据     <configuration> <configSections> <section name="hibernate-configuration&q ...

  7. 利用Teensy进行EM410x卡模拟以及暴力破解EM410X类门禁系统可行性猜想

    前些天Open入手了Teensy++2.0模拟EM410X,并且针对EM410X的门禁进行了一次暴力破解测试,以下就是相关代码以及内容. 什么是低频?什么是EM410x? 首先,我不得不再次提一下那些 ...

  8. cocos2d-x OpenGL ES 坐标系总结

    很多教程都说cocos2d-x OpenGL ES世界坐标系原点在左下角,但至于为什么在左下角却从来没有人提过,这导致大部分人觉得这是OpenGL ES的规定,事实上这是错的.OpenGL ES的坐标 ...

  9. 首先给大家介绍一下数据库project师,数据库project师(Database Engineer),是从事管理和维护数据库管理系统(DBMS)

    摘要 MySQL的最初的核心思想,主要是开源.简便易用.其开发可追溯至1985年,而第一个内部发行版本号诞生,已经是1995年. 到1998年,MySQL已经能够支持10中操作系统了.当中就包含win ...

  10. 简易推荐引擎的python实现

    代码地址如下:http://www.demodashi.com/demo/12913.html 主要思路 使用协同过滤的思路,从当前指定的用户过去的行为和其他用户的过去行为的相似度进行相似度评分,然后 ...