Skip to content

Commit e56a924

Browse files
committed
solve problem Number Of Boomerangs
1 parent 343e309 commit e56a924

File tree

5 files changed

+64
-0
lines changed

5 files changed

+64
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ All solutions will be accepted!
101101
|661|[Image Smoother](https://leetcode-cn.com/problems/image-smoother/description/)|[java/py/js](./algorithms/ImageSmoother)|Easy|
102102
|598|[Range Addition II](https://leetcode-cn.com/problems/range-addition-ii/description/)|[java/py/js](./algorithms/RangeAdditionII)|Easy|
103103
|492|[Construct The Rectangle](https://leetcode-cn.com/problems/construct-the-rectangle/description/)|[java/py/js]|(./algorithms/ConstructTheRectangle)|Easy|
104+
|447|[Number Of Boomerangs](https://leetcode-cn.com/problems/number-of-boomerangs/description/)|[java/py/js](./algorithms/NumberOfBoomerangs)|Easy|
104105

105106
# Database
106107
|#|Title|Solution|Difficulty|
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Number Of Boomerangs
2+
This problem is easy to solve by hashmap
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public int numberOfBoomerangs(int[][] points) {
3+
int count = 0;
4+
5+
for (int[] point : points) {
6+
Map<Integer, Integer> distanceMap = new HashMap<Integer, Integer>();
7+
for (int[] p : points) {
8+
int distance = (int) Math.pow(point[0] - p[0], 2) + (int) Math.pow(point[1] - p[1], 2);
9+
if (distanceMap.get(distance) == null) {
10+
distanceMap.put(distance, 1);
11+
} else {
12+
count += 2 * distanceMap.get(distance);
13+
distanceMap.put(distance, distanceMap.get(distance) + 1);
14+
}
15+
}
16+
}
17+
18+
return count;
19+
}
20+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @param {number[][]} points
3+
* @return {number}
4+
*/
5+
var numberOfBoomerangs = function(points) {
6+
let count = 0
7+
8+
points.forEach(point => {
9+
let distanceMap = new Map()
10+
points.forEach(p => {
11+
let distance = Math.pow(point[0] - p[0], 2) + Math.pow(point[1] - p[1], 2)
12+
if (distanceMap.get(distance) === undefined) {
13+
distanceMap.set(distance, 1);
14+
} else {
15+
count += 2 * distanceMap.get(distance)
16+
distanceMap.set(distance, distanceMap.get(distance) + 1)
17+
}
18+
})
19+
})
20+
21+
return count
22+
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution(object):
2+
def numberOfBoomerangs(self, points):
3+
"""
4+
:type points: List[List[int]]
5+
:rtype: int
6+
"""
7+
count = 0
8+
9+
for point in points:
10+
distance_map = {}
11+
for p in points:
12+
distance = pow(point[0] - p[0], 2) + pow(point[1] - p[1], 2)
13+
if distance_map.get(distance) == None:
14+
distance_map[distance] = 1
15+
else:
16+
count += 2 * distance_map[distance]
17+
distance_map[distance] += 1
18+
19+
return count

0 commit comments

Comments
 (0)