LeetCode Rectangle Area

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode Rectangle Area相关的知识,希望对你有一定的参考价值。

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

技术分享

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

 

以上是关于LeetCode Rectangle Area的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode 939. Minimum Area Rectangle

LeetCode Rectangle Area

LeetCode 963. Minimum Area Rectangle II

leetcode 223: Rectangle Area

Leetcode 223 Rectangle Area

LeetCode - Minimum Area Rectangle