[leetcode_223]Rectangle Area

计算两个矩形的面积和,有可能有重叠区域。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
class Solution {
public:
    int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
        int bx,by,tx,ty;
        bx = A > E ? A : E;
        by = B > F ? B : F;
        tx = C < G ? C : G;
        ty = D < H ? D : H;

        int same = 0;
        if (tx <= bx || ty <= by) {
            same =  0;
        } else {
            same = (tx - bx) * (ty - by);
        }

        return (C - A) * (D - B) + (G - E) * (H - F) - same;
    }
};
Licensed under CC BY-NC-SA 4.0