leetcode-223-rectangle area

Problem Description:

Find the total area covered by two rectilinear rectangles in a 2D plane.

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.

题目大意:

求出两个矩形覆盖的面积值。

Solutions:

分重叠与无重叠两种情况讨论,很简单。

Code in C++:

class Solution {
public:
    int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
        int AreaA = (C-A)*(D-B);
        int AreaB = (G-E)*(H-F);
        if(A>=G||C<=E||B>=H||F>=D) return AreaA+AreaB;
        int cover = max(min(C,G)-max(A,E),0)*max(min(D,H)-max(F,B),0);
        return AreaA+AreaB-cover;
    }
};