223. Rectangle Area

class Solution {
public:
    int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
        int res = (C-A) * (D-B) + (G-E) * (H-F);
        long x1 = max(A,E), y1 = max(B,F), x2 = min(C,G), y2 = min(D,H);
        return res - max(0L, x2-x1) * max(0L,y2-y1);  // x2-x1 may overflow
    }
};

 

class Solution {
public:
    int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
        int res = (C-A) * (D-B) + (G-E) * (H-F);
        int x1 = max(A,E), y1 = max(B,F), x2 = min(C,G), y2 = min(D,H);
        if (x1 < x2 && y1 < y2)
            res -= (x2-x1) * (y2-y1);
        return res;
    }
};

 

posted @ 2018-11-30 15:44  JTechRoad  阅读(91)  评论(0编辑  收藏  举报