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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值