zoukankan      html  css  js  c++  java
  • 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 iand 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:

    [[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]]
    
    class Solution {
    public:
        int numberOfBoomerangs(vector<pair<int, int>>& points) {//用map来记录到某个点为某个距离点的个数
            int res=0;
            for(int i=0; i<points.size(); i++){
                unordered_map<long,int> group(points.size());
                for(int j=0; j<points.size(); j++){
                    if(i==j) continue;
                    int p=(points[i].first-points[j].first)*(points[i].first-points[j].first);
                    int q=(points[i].second-points[j].second)*(points[i].second-points[j].second);
                    group[p+q]++;
                }
                for(auto o:group)
                    if(o.second>1)
                       res+=o.second*(o.second-1);
            }
            return res;
        }
    };
    
  • 相关阅读:
    Identifier expected after this token
    需要整理的
    Context
    SharedPreferences
    一些常规注意事项
    一个点亮屏幕的service
    BroadcastReceiver中调用Service
    BroadcastReceiver
    Service
    微服务简介
  • 原文地址:https://www.cnblogs.com/A-Little-Nut/p/10066947.html
Copyright © 2011-2022 走看看