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 }
  • 相关阅读:
    Linux三剑客之sed
    xcodebuild
    mac xcworkspace xcodebuild
    [转]Jenkins Xcode打包ipa
    ios批量打包
    ios打包
    ios 打包
    ios 尺寸
    Launch Screen在iOS7/8中的实现
    如何查看ipa包支持哪些cpu指令集
  • 原文地址:https://www.cnblogs.com/amazingzoe/p/6722641.html
Copyright © 2011-2022 走看看