To check if all points are reflected over a vertical line, we note that the reflection line must be at x = (minX + maxX) / 2. For every point (x, y), its reflection is (minX + maxX - x, y). If all such reflected points exist in the set, the answer is true.
classSolution {
publicbooleanisReflected(int[][] points) {
int min = Integer.MAX_VALUE, max = Integer.MIN_VALUE;
Set<String> set =new HashSet<>();
for (int[] p : points) {
min = Math.min(min, p[0]);
max = Math.max(max, p[0]);
set.add(p[0]+","+ p[1]);
}
int sum = min + max;
for (int[] p : points) {
int rx = sum - p[0];
if (!set.contains(rx +","+ p[1])) returnfalse;
}
returntrue;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
classSolution {
funisReflected(points: Array<IntArray>): Boolean {
var min = Int.MAX_VALUE; var max = Int.MIN_VALUE
val set = mutableSetOf<String>()
for (p in points) {
min = minOf(min, p[0]); max = maxOf(max, p[0])
set.add("${p[0]},${p[1]}")
}
val sum = min + max
for (p in points) {
val rx = sum - p[0]
if ("$rx,${p[1]}"!inset) returnfalse }
returntrue }
}
1
2
3
4
5
6
7
8
9
10
classSolution:
defisReflected(self, points: list[list[int]]) -> bool:
min_x = min(p[0] for p in points)
max_x = max(p[0] for p in points)
s = set((p[0], p[1]) for p in points)
sum_x = min_x + max_x
for x, y in points:
if (sum_x - x, y) notin s:
returnFalsereturnTrue
1
2
3
4
5
6
7
8
9
10
11
12
13
use std::collections::HashSet;
impl Solution {
pubfnis_reflected(points: Vec<Vec<i32>>) -> bool {
let min_x = points.iter().map(|p| p[0]).min().unwrap();
let max_x = points.iter().map(|p| p[0]).max().unwrap();
let s: HashSet<(i32, i32)>= points.iter().map(|p| (p[0], p[1])).collect();
let sum = min_x + max_x;
for p in&points {
if!s.contains(&(sum - p[0], p[1])) { returnfalse; }
}
true }
}