码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode 391: Perfect Square

时间:2017-10-15 17:49:35      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:span   int   red   point   pre   math   contain   logs   boolean   

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));
    }
}

 

LeetCode 391: Perfect Square

标签:span   int   red   point   pre   math   contain   logs   boolean   

原文地址:http://www.cnblogs.com/shuashuashua/p/7672438.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!