Note:

All the points should be even since it will be dispeared by perfect joint. Thus only 4 points will be left.

 

class Solution {
    public boolean isRectangleCover(int[][] rectangles) {
        int lx = Integer.MAX_VALUE, ly = Integer.MAX_VALUE, rx = 0, ry = 0, sum = 0;
        HashSet<String> set = new HashSet<String>();
        for (int[] rec : rectangles) {
            lx = Math.min(rec[0], lx);
            ly = Math.min(rec[1], ly);
            rx = Math.max(rec[2], rx);
            ry = Math.max(rec[3], ry);
            sum += (rec[2] - rec[0]) * (rec[3] - rec[1]);
            String s1 = rec[0] + " " + rec[1];
            String s2 = rec[0] + " " + rec[3];
            String s3 = rec[2] + " " + rec[3];
            String s4 = rec[2] + " " + rec[1];
            
            if (!set.add(s1)) set.remove(s1);
            if (!set.add(s2)) set.remove(s2);
            if (!set.add(s3)) set.remove(s3);
            if (!set.add(s4)) set.remove(s4);
        }
        
        if (!set.contains(lx + " " + ly) || !set.contains(lx + " " + ry) || !set.contains(rx + " " + ly) || !set.contains(rx + " " + ry) || set.size() != 4) return false;
            
        
        return sum == ((rx - lx) * (ry - ly));
    }
}

 

posted on 2017-10-15 15:52  keepshuatishuati  阅读(157)  评论(0编辑  收藏  举报