356. Line Reflection

首先找到X方向的中点,如果中点是一个点,那么分别从这个点开始往左右找就行;如果是一个区间,比如1 2之间,那么首先总点数得是偶数,然后以1和2往左右两边找就行。。

找的时候,有3种情况:
同时没找到,继续;
一个找到,一个没找到,FALSE;
同时找到,左边找到的每个点,必须对应一个右边找到的每个点,纵坐标相同。

所以构架的时候,我用的
Map<Integer,Set>
Key是X坐标,Value是一个SET,表示横坐标为X的所有点。

public class Solution {
    public boolean isReflected(int[][] points) 
    {
        Map<Integer,Set<Integer>> map = new HashMap<Integer,Set<Integer>>();
        
        int max = Integer.MIN_VALUE;
        int min = Integer.MAX_VALUE;
        for(int i = 0; i < points.length; i++)
        {
            int x = points[i][0];
            max = Math.max(x,max);
            min = Math.min(x,min);
            int y = points[i][1];
            if(map.containsKey(x))
            {
                map.get(x).add(y);
            }
            else
            {
                Set<Integer> tempSet = new HashSet<>();
                tempSet.add(y);
                map.put(x,tempSet);
            }
        }
        int left;
        int right;
        
        // mid line is between 2 points
        if((max+min) % 2 != 0)
        {
            if(points.length%2 != 0) return false;
            if(max+min> 0)
            {
                left = (max+min)/2;
                right = (max+min)/2 + 1;
            }
            else
            {
                right = (max+min)/2;
                left = right -1;
            }
        }
        else
        {
            left = (max+min)/2;
            right = left;

        }

        
        while(left >= min && right <= max)
        {
            if(map.containsKey(left) && map.containsKey(right))
            {
                Set<Integer> l = map.get(left);
                Set<Integer> r = map.get(right);
                if(l.size() != r.size()) return false;
                
                for(Integer i: l)
                {
                    if(!r.contains(i)) return false;
                }
                
                left--;
                right++;
                
                
            }
            else if(map.containsKey(left) || map.containsKey(right))
            {
                return false;
            }
            else
            {
                left--;
                right++;
            }
        }
        
        return left < min && right > max;
    }
}

提示好像也是这么个意思。。但是没搞懂提示里说的N²是怎么个做法。。

原文地址:https://www.cnblogs.com/reboot329/p/5951391.html