LeetCode Rectangle Area

Each rectangle is defined by its bottom left corner and top right corner as shown in the figure.

Rectangle Area

Assume that the total area is never beyond the maximum possible value of int.

这个题目分两种情况,第一种是两者不重合,则结果为两个矩形面积之和,第二种是两者重合,则结果为两个矩形面积之和减去重叠的部分

class Solution {
public:
    int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
        int area_AD=(C-A)*(D-B);
        int area_EH=(G-E)*(H-F);
        int max_AE=(A>E)?A:E;
        int max_BF=(B>F)?B:F;
        int min_CG=(C>G)?G:C;
        int min_DH=(D>H)?H:D;
        if(C<=E||D<=F||G<=A||H<=B)
        return area_AD+area_EH;
        else
        return area_AD+area_EH-(min_CG-max_AE)*(min_DH-max_BF);
    }
};
原文地址:https://www.cnblogs.com/csudanli/p/5335558.html