LeetCode 391. 完美矩形

题目链接

思路:如果是完美矩形,则所有矩形中最外面的四个坐标为该矩形的四个顶点,所有小矩形的面积和等于完美矩形的面积。对于剩余的点,一定会成对出现(四次或者两次),若超过四次则会重叠,奇数次则没有完全覆盖。

代码:

class Solution {
    public boolean isRectangleCover(int[][] rectangles) {
        int x = Integer.MAX_VALUE;
        int y = Integer.MAX_VALUE;
        int a = Integer.MIN_VALUE;
        int b = Integer.MIN_VALUE;
        int smallRecs = 0;
        Set<String> set = new HashSet<>();
        for(int[] r:rectangles){
            x = Math.min(r[0],x);
            y = Math.min(r[1],y);
            a = Math.max(r[2],a);
            b = Math.max(r[3],b);
            smallRecs +=(r[2]-r[0])*(r[3]-r[1]);
            String lt = r[0]+","+r[3];
            String lb = r[0]+","+r[1];
            String rt = r[2]+","+r[3];
            String rb = r[2]+","+r[1];
            if(set.contains(lt)) set.remove(lt);
            else set.add(lt);
            if(set.contains(lb)) set.remove(lb);
            else set.add(lb);
            if(set.contains(rt)) set.remove(rt);
            else set.add(rt);
            if(set.contains(rb)) set.remove(rb);
            else set.add(rb);
        }
        if(set.size()==4&&set.contains(x+","+b)&&set.contains(x+","+y)
&&set.contains(a+","+b)&&set.contains(a+","+y)) return smallRecs==(a-x)*(b-y);
        return false;
    }
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值