力扣223. 矩形面积

该博客主要介绍了如何解决LeetCode上的矩形面积计算问题,包括计算两个矩形的总面积以及它们相交部分的面积。通过定义布尔变量`flag`和辅助方法`both`来确定矩形是否相交,并计算相交部分的边界。最终返回总面积减去相交面积的结果,实现精确的几何计算。
摘要由CSDN通过智能技术生成

 223. 矩形面积 - 力扣(LeetCode) (leetcode-cn.com)

class Solution {
    boolean flag = false;
    public int computeArea(int ax1, int ay1, int ax2, int ay2, int bx1, int by1, int bx2, int by2) {
        int area1 = (ax2-ax1)*(ay2-ay1);
        int area2 = (bx2-bx1)*(by2-by1);
        int[] weight = both(ax1,ax2,bx1,bx2);
        int[] lo = both(ay1,ay2,by1,by2);
        if(flag)return area1+area2;
        int bothweight = weight[1]-weight[0];
        int bothlo = lo[1]-lo[0];
        int botharea = bothlo*bothweight;
        return area1+area2-botharea;

    }
    
    public int[] both(int x11,int x12,int x21,int x22){
        int[] res = new int[2];
        if(x12<=x21||x22<x11){
            flag = true;
            return res;
        }
        else {
             res[0] = Math.max(x11,x21);
             res[1] = Math.min(x12,x22);
        }
        return res;
    }
}

九月份打卡全部完成!

纪念一下

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值