356. Line Reflection

51 篇文章 0 订阅
50 篇文章 0 订阅

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)?

Hint:

  1. Find the smallest and largest x-value for all points.
  2. If there is a line then it should be at y = (minX + maxX) / 2.
  3. For each point, make sure that it has a reflected point in the opposite side.

Credits:
Special thanks to @memoryless for adding this problem and creating all test cases.

解题思路在hint中已经给出,关键是怎么求opposit point,和怎么存point了。开始是新建一个point类,用hashmap<integer, list<point>>存point。求opposit point的时候先算中点,再用中点求opposit point,这样就得用double计算。以上两种情况是最初思路,代码如下:

public class Solution {
    private class Point {
        int x;
        int y;
        public Point(int px, int py) {
            this.x = px;
            this.y = py;
        }
    }
    
    public boolean isReflected(int[][] points) {
        if (points == null || points.length == 0 || points[0].length == 0) {
            return true;
        }
        double minX = Integer.MAX_VALUE, maxX = Integer.MIN_VALUE;
        HashMap<Integer, List<Point>> map = new HashMap<Integer, List<Point>>();
        for (int[] point: points) {
            if (point[0] < minX) {
                minX = (double)point[0];
            }
            if (point[0] > maxX) {
                maxX = (double)point[0];
            }
            if (!map.containsKey(point[0]))
                map.put(point[0], new ArrayList<Point>());
            map.get(point[0]).add(new Point(point[0], point[1]));
        }
        double mid = minX + (maxX - minX) / 2;
        for (int[] point: points) {
            double reflect = mid + mid - point[0];
            boolean find = false;
            if (map.containsKey((int)reflect)) {
                for (Point p: map.get((int)reflect)) {
                    if (point[1] == p.y) {
                        find = true;
                    }
                }
                if (find == false) {
                    return false;
                }
            } else {
                return false;
            }
        }
        return true;
    }
}
有两点可以改进:第一,改用HashSet.add(Arrays.hashCode(point)) 存点;第二,求对称点时,用minX + maxX求出和,因为是对称点,中点都是一样的,那么sum也肯定是一样的,sum减其中一个点的x,就是另一个点的x。代码如下:

public class Solution {
    public boolean isReflected(int[][] points) {
        HashSet<Integer> pointSet = new HashSet<>();
        int sum;
        int maxX, minX;
        
        minX = Integer.MAX_VALUE;
        maxX = Integer.MIN_VALUE;
        for(int[] point:points) {
            maxX = Math.max(maxX, point[ 0 ]);
            minX = Math.min(minX, point[ 0 ]);
            pointSet.add(Arrays.hashCode(point));
        }
        
        sum = maxX+minX;
        for(int[] point:points) {
            if(!pointSet.contains(Arrays.hashCode(new int[]{sum-point[ 0 ], point[ 1 ]}))) {
                return false;
            }
        }
        return true;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值