leetcode 447 Number of Boomerangs
每一轮for循环,判断当points[i]为Boomerangs的第一个point时,第二个point和第三个point有几种情况:计算points[i]与其他每一个points[j]的距离;当距离相等的点大于等于两个时,从这些点中选两个,且则两个点有序(组合问题),就是一种情况。
class Solution { public: int numberOfBoomerangs(vector<vector<int>>& points) { int re=0; for(int i=0;i<points.size();++i) { unordered_map<int,int> m; for(int j=0;j<points.size();++j) { if(i==j) continue; int dis=(points[i][0]-points[j][0])*(points[i][0]-points[j][0])+(points[i][1]-points[j][1])*(points[i][1]-points[j][1]); ++m[dis]; } for(auto& t:m) { if(t.second==1) continue; int ans=t.second*(t.second-1); re+=ans; } } return re; } };