File tree 5 files changed +131
-6
lines changed
0300-0399/0356.Line Reflection
0400-0499/0447.Number of Boomerangs
5 files changed +131
-6
lines changed Original file line number Diff line number Diff line change 47
47
48
48
先找出所有点中的最小、最大的 x 坐标 ` minX ` 和 ` maxX ` 。若存在满足条件的直线,则直线 ` x = (minX + maxX) / 2 ` 。(或者说:` s = minX + maxX ` )
49
49
50
- 遍历每个点 ` point(x, y) ` ,若 (s - x, y) 不在点集里,说明不满足条件,直接返回 false。遍历结束返回 true。
50
+ 遍历每个点 ` point(x, y) ` ,若 ` (s - x, y) ` 不在点集里,说明不满足条件,直接返回 false。遍历结束返回 true。
51
51
52
52
<!-- tabs:start -->
53
53
Original file line number Diff line number Diff line change 45
45
<li>所有点都 <strong>互不相同</strong></li>
46
46
</ul >
47
47
48
-
49
48
## 解法
50
49
51
50
<!-- 这里可写通用的实现逻辑 -->
52
51
52
+ 计数器实现。
53
+
54
+ 对于每个点,计算其他点到该点的距离,然后按照距离进行分组计数。对每个组中的点进行两两排列组合(A n 取 2,即 ` n * (n - 1)) ` )计数即可。
55
+
53
56
<!-- tabs:start -->
54
57
55
58
### ** Python3**
56
59
57
60
<!-- 这里可写当前语言的特殊实现逻辑 -->
58
61
59
62
``` python
60
-
63
+ class Solution :
64
+ def numberOfBoomerangs (self , points : List[List[int ]]) -> int :
65
+ n = len (points)
66
+ if len (points) < 3 :
67
+ return 0
68
+ number = 0
69
+ for i in range (n):
70
+ distance_counter = collections.Counter()
71
+ for j in range (n):
72
+ if i == j:
73
+ continue
74
+ x1, y1 = points[i][0 ], points[i][1 ]
75
+ x2, y2 = points[j][0 ], points[j][1 ]
76
+ distance = (x1 - x2) ** 2 + (y1 - y2) ** 2
77
+ distance_counter[distance] += 1
78
+ number += sum ([val * (val - 1 ) for val in distance_counter.values()])
79
+ return number
61
80
```
62
81
63
82
### ** Java**
64
83
65
84
<!-- 这里可写当前语言的特殊实现逻辑 -->
66
85
67
86
``` java
68
-
87
+ class Solution {
88
+ public int numberOfBoomerangs (int [][] points ) {
89
+ int n = points. length;
90
+ if (n < 3 ) {
91
+ return 0 ;
92
+ }
93
+ int number = 0 ;
94
+ for (int i = 0 ; i < n; ++ i) {
95
+ Map<Integer , Integer > distanceCounter = new HashMap<> ();
96
+ for (int j = 0 ; j < n; ++ j) {
97
+ if (i == j) {
98
+ continue ;
99
+ }
100
+ int x1 = points[i][0 ], y1 = points[i][1 ];
101
+ int x2 = points[j][0 ], y2 = points[j][1 ];
102
+ int distance = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
103
+ distanceCounter. put(distance, distanceCounter. getOrDefault(distance, 0 ) + 1 );
104
+ }
105
+ for (int val : distanceCounter. values()) {
106
+ number += val * (val - 1 );
107
+ }
108
+ }
109
+ return number;
110
+ }
111
+ }
69
112
```
70
113
71
114
### ** ...**
Original file line number Diff line number Diff line change 50
50
### ** Python3**
51
51
52
52
``` python
53
-
53
+ class Solution :
54
+ def numberOfBoomerangs (self , points : List[List[int ]]) -> int :
55
+ n = len (points)
56
+ if len (points) < 3 :
57
+ return 0
58
+ number = 0
59
+ for i in range (n):
60
+ distance_counter = collections.Counter()
61
+ for j in range (n):
62
+ if i == j:
63
+ continue
64
+ x1, y1 = points[i][0 ], points[i][1 ]
65
+ x2, y2 = points[j][0 ], points[j][1 ]
66
+ distance = (x1 - x2) ** 2 + (y1 - y2) ** 2
67
+ distance_counter[distance] += 1
68
+ number += sum ([val * (val - 1 ) for val in distance_counter.values()])
69
+ return number
54
70
```
55
71
56
72
### ** Java**
57
73
58
74
``` java
59
-
75
+ class Solution {
76
+ public int numberOfBoomerangs (int [][] points ) {
77
+ int n = points. length;
78
+ if (n < 3 ) {
79
+ return 0 ;
80
+ }
81
+ int number = 0 ;
82
+ for (int i = 0 ; i < n; ++ i) {
83
+ Map<Integer , Integer > distanceCounter = new HashMap<> ();
84
+ for (int j = 0 ; j < n; ++ j) {
85
+ if (i == j) {
86
+ continue ;
87
+ }
88
+ int x1 = points[i][0 ], y1 = points[i][1 ];
89
+ int x2 = points[j][0 ], y2 = points[j][1 ];
90
+ int distance = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
91
+ distanceCounter. put(distance, distanceCounter. getOrDefault(distance, 0 ) + 1 );
92
+ }
93
+ for (int val : distanceCounter. values()) {
94
+ number += val * (val - 1 );
95
+ }
96
+ }
97
+ return number;
98
+ }
99
+ }
60
100
```
61
101
62
102
### ** ...**
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int numberOfBoomerangs (int [][] points ) {
3
+ int n = points .length ;
4
+ if (n < 3 ) {
5
+ return 0 ;
6
+ }
7
+ int number = 0 ;
8
+ for (int i = 0 ; i < n ; ++i ) {
9
+ Map <Integer , Integer > distanceCounter = new HashMap <>();
10
+ for (int j = 0 ; j < n ; ++j ) {
11
+ if (i == j ) {
12
+ continue ;
13
+ }
14
+ int x1 = points [i ][0 ], y1 = points [i ][1 ];
15
+ int x2 = points [j ][0 ], y2 = points [j ][1 ];
16
+ int distance = (x1 - x2 ) * (x1 - x2 ) + (y1 - y2 ) * (y1 - y2 );
17
+ distanceCounter .put (distance , distanceCounter .getOrDefault (distance , 0 ) + 1 );
18
+ }
19
+ for (int val : distanceCounter .values ()) {
20
+ number += val * (val - 1 );
21
+ }
22
+ }
23
+ return number ;
24
+ }
25
+ }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def numberOfBoomerangs (self , points : List [List [int ]]) -> int :
3
+ n = len (points )
4
+ if len (points ) < 3 :
5
+ return 0
6
+ number = 0
7
+ for i in range (n ):
8
+ distance_counter = collections .Counter ()
9
+ for j in range (n ):
10
+ if i == j :
11
+ continue
12
+ x1 , y1 = points [i ][0 ], points [i ][1 ]
13
+ x2 , y2 = points [j ][0 ], points [j ][1 ]
14
+ distance = (x1 - x2 ) ** 2 + (y1 - y2 ) ** 2
15
+ distance_counter [distance ] += 1
16
+ number += sum ([val * (val - 1 ) for val in distance_counter .values ()])
17
+ return number
You can’t perform that action at this time.
0 commit comments