前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【LeetCode 204】关关的刷题日记40 Number of Boomerangs

【LeetCode 204】关关的刷题日记40 Number of Boomerangs

作者头像
WZEARW
发布2018-04-10 17:48:37
7360
发布2018-04-10 17:48:37
举报
文章被收录于专栏:专知

关关的刷题日记40 – Leetcode 447. Number of Boomerangs

题目

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.

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]]

题目的意思是一个平面上有n个点,一个"boomerang"由三个点(i, j, k)构成,i和j之间的距离等于i和k之间的距离,让我们求这n个点构成的"boomerang"的数量。

思路

思路:对于任意一个点P来说,如果到3个点B、C、D的距离相等,那么会构成A32 个"boomerang",分别是 A B C,A C B,A B D,A D B,A C D,A D C,同样对于任意一个点,如果到n个点距离相等,那么会构成An2 个"boomerang"。我们可以遍历数组中的每一个点,对于任意一个点,求出到它距离相等的点的个数,根据个数求出"boomerang"的数量,对数量进行累加,得到最终结果。

代码语言:javascript
复制
class Solution {public:
    int numberOfBoomerangs(vector<pair<int, int>>& points) {
        int n=points.size(), re=0, dis=0;
        for(int i=0;i<n;i++)
        {
            map<int,int>m;
            for(int j=0; j<n; j++)
            {
                dis=(points[i].first-points[j].first)*(points[i].first-points[j].first)+(points[i].second-points[j].second)*(points[i].second-points[j].second);
                m[dis]++;
            }
            for(auto x:m)
            {
                if(x.second>=2)
                    re+=(x.second-1)*x.second;
            }
        }
        return re;
    }};

人生易老,唯有陪伴最长情,加油!

以上就是关关关于这道题的总结经验,希望大家能够理解,有什么问题可以在我们的专知公众号平台上交流或者加我们的QQ专知-人工智能交流群 426491390,也可以加入专知——Leetcode刷题交流群(请先加微信小助手weixinhao: Rancho_Fang)。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2017-11-19,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 专知 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题目
  • 思路
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档