45
45
46
46
<!-- 这里可写通用的实现逻辑 -->
47
47
48
- 先找出所有点中的最小、最大的 x 坐标 ` minX ` 和 ` maxX ` 。若存在满足条件的直线,则直线 ` x = (minX + maxX) / 2 ` 。(或者说: ` s = minX + maxX ` )
48
+ ** 方法一:哈希表 **
49
49
50
- 遍历每个点 ` point(x, y) ` ,若 ` (s - x, y) ` 不在点集里,说明不满足条件,直接返回 false。遍历结束返回 true。
50
+ 我们先找出所有点中的最小、最大的 $x$ 坐标 $minX$ 和 $maxX$。若存在满足条件的直线,则直线 $x = (minX + maxX) / 2$,或者说 $s = minX + maxX$。
51
+
52
+ 接下来,我们遍历每个点 $(x, y),若 $(s - x, y)$ 不在点集里,说明不满足条件,直接返回 ` false ` 。遍历结束返回 ` true ` 。
53
+
54
+ 时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 $points$ 的长度。
51
55
52
56
<!-- tabs:start -->
53
57
@@ -65,10 +69,7 @@ class Solution:
65
69
max_x = max (max_x, x)
66
70
point_set.add((x, y))
67
71
s = min_x + max_x
68
- for x, y in points:
69
- if (s - x, y) not in point_set:
70
- return False
71
- return True
72
+ return all ((s - x, y) in point_set for x, y in points)
72
73
```
73
74
74
75
### ** Java**
@@ -78,21 +79,83 @@ class Solution:
78
79
``` java
79
80
class Solution {
80
81
public boolean isReflected (int [][] points ) {
81
- int minX = Integer . MAX_VALUE , maxX = Integer . MIN_VALUE ;
82
- Set<String > pointSet = new HashSet<> ();
83
- for (int [] point : points) {
84
- minX = Math . min(minX, point[0 ]);
85
- maxX = Math . max(maxX, point[0 ]);
86
- pointSet. add(point[0 ] + " ." + point[1 ]);
82
+ final int inf = 1 << 30 ;
83
+ int minX = inf, maxX = - inf;
84
+ Set<List<Integer > > pointSet = new HashSet<> ();
85
+ for (int [] p : points) {
86
+ minX = Math . min(minX, p[0 ]);
87
+ maxX = Math . max(maxX, p[0 ]);
88
+ pointSet. add(List . of(p[0 ], p[1 ]));
89
+ }
90
+ int s = minX + maxX;
91
+ for (int [] p : points) {
92
+ if (! pointSet. contains(List . of(s - p[0 ], p[1 ]))) {
93
+ return false ;
94
+ }
95
+ }
96
+ return true ;
97
+ }
98
+ }
99
+ ```
100
+
101
+ ### ** C++**
102
+
103
+ ``` cpp
104
+ class Solution {
105
+ public:
106
+ bool isReflected(vector<vector<int >>& points) {
107
+ const int inf = 1 << 30;
108
+ int minX = inf, maxX = -inf;
109
+ set<pair<int, int>> pointSet;
110
+ for (auto& p : points) {
111
+ minX = min(minX, p[ 0] );
112
+ maxX = max(maxX, p[ 0] );
113
+ pointSet.insert({p[ 0] , p[ 1] });
87
114
}
88
- long s = minX + maxX;
89
- for (int [] point : points) {
90
- if (! pointSet. contains(( s - point [0 ]) + " . " + point [1 ])) {
115
+ int s = minX + maxX;
116
+ for (auto& p : points) {
117
+ if (!pointSet.count({ s - p [ 0] , p [ 1] } )) {
91
118
return false;
92
119
}
93
120
}
94
121
return true;
95
122
}
123
+ };
124
+ ```
125
+
126
+ ### **Go**
127
+
128
+ ```go
129
+ func isReflected(points [][]int) bool {
130
+ const inf = 1 << 30
131
+ minX, maxX := inf, -inf
132
+ pointSet := map[[2]int]bool{}
133
+ for _, p := range points {
134
+ minX = min(minX, p[0])
135
+ maxX = max(maxX, p[0])
136
+ pointSet[[2]int{p[0], p[1]}] = true
137
+ }
138
+ s := minX + maxX
139
+ for _, p := range points {
140
+ if !pointSet[[2]int{s - p[0], p[1]}] {
141
+ return false
142
+ }
143
+ }
144
+ return true
145
+ }
146
+
147
+ func min(a, b int) int {
148
+ if a < b {
149
+ return a
150
+ }
151
+ return b
152
+ }
153
+
154
+ func max(a, b int) int {
155
+ if a > b {
156
+ return a
157
+ }
158
+ return b
96
159
}
97
160
```
98
161
0 commit comments