forked from xiaoyaoworm/Leetcode-java
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path356_isReflected.java
29 lines (27 loc) · 1.01 KB
/
356_isReflected.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//find minX and maxX, mid must be middle of them,
//use String as set element is better. find whether reflection is there.
//O(N) time
public class Solution {
public boolean isReflected(int[][] points) {
if(points == null || points.length == 0) return true;
int minX = Integer.MAX_VALUE;
int maxX = Integer.MIN_VALUE;
int m = points.length;
HashSet<String> set = new HashSet<String>();
for(int i = 0; i < m; i++){
minX = Math.min(minX, points[i][0]);
maxX = Math.max(maxX, points[i][0]);
set.add(points[i][0]+":"+points[i][1]); //Use String in set is good!!
}
double midX = minX+((double)maxX-(double)minX)/2;
for(int[] point: points){
if(point[0] == midX) continue;
int x = point[0];
int y = point[1];
int newX = (int)(midX+midX-x);
String str = newX + ":" + point[1];
if(!set.contains(str)) return false;
}
return true;
}
}