letecode [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 jequals 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]]

Subject to the effect :  

  A tuple (i, j, k) set on a plane different points n "boomerang" is represented by a point, wherein the distance between the distance between the i and j and i and k are equal (taking into account membered order for the group). Find the number of all boomerang.

Understanding:

  Fixed point i, calculate its distance from other nodes. Save with ordered_map. If the distance from the point x i is dis, the arrangement method is x * (x-1) thereof.

  ordered_map If a key is present can be accessed, then the absence of data is inserted, value defaults to zero. ordered_map [key].

Code C ++:

class Solution {
public:
    int numberOfBoomerangs(vector<vector<int>>& points) {
        int sum = 0,x,y;
        for(int i=0;i<points.size();++i){
            unordered_map<int,int> couple;
            for(int j=0;j<points.size();++j){
                x = (points[i][0]-points[j][0]);
                y = (points[i][1]-points[j][1]);
                ++couple[x*x+y*y];
            }
            for(auto itr=couple.begin();itr!=couple.end();itr++){
                sum += itr->second*(itr->second - 1);
            }
        }
        return sum;
    }
};

operation result:

  When execution: 644 ms, beat the 83.13% of all users to submit in C ++

  Memory consumption: 134 MB, defeated 29.54% of all users to submit in C ++

Guess you like

Origin www.cnblogs.com/lpomeloz/p/11099597.html