zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    复习正则表达式20190618
    python每日练习10题2
    java多线程
    资源2
    apache
    行转列,列转行
    mysql5.7安装(正确安装)实战
    常见规则引擎技术
    Spark性能优化之道——解决Spark数据倾斜(Data Skew)的N种姿势
    Vue开源项目库汇总
  • 原文地址:https://www.cnblogs.com/amazingzoe/p/6722641.html
Copyright © 2011-2022 走看看