https://leetcode.com/problems/number-of-boomerangs/description/
Solution 1.
int numberOfBoomerangs(vector<pair<int, int>>& points) {
int d, res = 0;
for(auto &x : points) {
unordered_map<int,int> vm(points.size());
for(auto &y : points) {
d = pow(x.first - y.first, 2) + pow(x.second - y.second, 2);
res += 2 * vm[d]++;
}
}
return res;
}
Solution 2.
int numberOfBoomerangs(vector<pair<int, int>>& points) {
int d, res = 0;
for(int i=0; i<points.size(); i++) {
unordered_map<int,int> vm(points.size());
for(int j=0; j<points.size(); j++) {
d = pow(points[i].first - points[j].first, 2) + pow(points[i].second - points[j].second, 2);
vm[d]++;
}
for(auto &x: vm){
res += x.second * (x.second - 1);
}
}
return res;
}
No comments:
Post a Comment