File tree 5 files changed +64
-0
lines changed
algorithms/NumberOfBoomerangs
5 files changed +64
-0
lines changed Original file line number Diff line number Diff line change @@ -101,6 +101,7 @@ All solutions will be accepted!
101
101
| 661| [ Image Smoother] ( https://leetcode-cn.com/problems/image-smoother/description/ ) | [ java/py/js] ( ./algorithms/ImageSmoother ) | Easy|
102
102
| 598| [ Range Addition II] ( https://leetcode-cn.com/problems/range-addition-ii/description/ ) | [ java/py/js] ( ./algorithms/RangeAdditionII ) | Easy|
103
103
| 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|
104
105
105
106
# Database
106
107
| #| Title| Solution| Difficulty|
Original file line number Diff line number Diff line change
1
+ # Number Of Boomerangs
2
+ This problem is easy to solve by hashmap
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ } ;
Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments