Line Reflection

Given n points on a 2D plane, find if there is such a line parallel to y-axis that reflect the given points.

Example 1:

Given points = [[1,1],[-1,1]], return true.

Example 2:

Given points = [[1,1],[-1,-1]], return false.

Follow up:
Could you do better than O(n2)?

Analysis: If there is a line parallel to Y-axis, then the left-most and the right-most must be reflected by that line. 

 1 public class Solution {
 2     public boolean isReflected(int[][] points) {
 3         if (points == null) return true;
 4         int n = points.length;
 5         if (n < 2) return true;
 6         
 7         HashSet<String> set = new HashSet<String>();
 8         int maxValue = Integer.MIN_VALUE, minValue = Integer.MAX_VALUE;
 9         for (int i = 0; i < n; i++) {
10             maxValue = Math.max(maxValue, points[i][0]);
11             minValue = Math.min(minValue, points[i][0]);
12             set.add(points[i][0] + "," + points[i][1]);
13         }
14         
15         int sum = maxValue + minValue;
16         for (int i = 0; i < n; i++) {
17             String findMe = (sum - points[i][0]) + "," + points[i][1];
18             if (!set.contains(findMe)) return false;
19         }
20         return true;
21     }
22 }
原文地址:https://www.cnblogs.com/amazingzoe/p/6722641.html