Number of Boomerangs Solutions in C++
Number 447
Difficulty Easy
Acceptance 51.9%
Link LeetCode
Solutions
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/number-of-boomerangs/description//// Author : liuyubobobo/// Time : 2017-11-15#include <iostream>#include <vector>#include <unordered_map>#include <cassert>#include <stdexcept>using namespace std;/// Using Hash Map/// Time Complexity: O(n^2)/// Space Complexity: O(n)class Solution {public:int numberOfBoomerangs(vector<vector<int>>& points) {int res = 0;for( int i = 0 ; i < points.size() ; i ++ ){// record中存储 点i 到所有其他点的距离出现的频次unordered_map<int, int> record;for(int j = 0 ; j < points.size() ; j ++)if(j != i)// 计算距离时不进行开根运算, 以保证精度record[dis(points[i], points[j])] += 1;for(unordered_map<int, int>::iterator iter = record.begin() ; iter != record.end() ; iter ++)res += (iter->second) * (iter->second - 1);}return res;}private:int dis(const vector<int> &pa, const vector<int> &pb){return (pa[0] - pb[0]) * (pa[0] - pb[0]) +(pa[1] - pb[1]) * (pa[1] - pb[1]);}};int main() {vector<pair<int,int>> vec;vec.push_back(make_pair(0, 0));vec.push_back(make_pair(1, 0));vec.push_back(make_pair(2, 0));cout << Solution().numberOfBoomerangs(vec) << endl;return 0;}