LeetCode Rectangle Area

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.

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 left = max(A, E);
        int bottom = max(B, F);
        int right = min(C, G);
        int top = min(D, H);
        int shared = max(right - left, 0) * max(top - bottom, 0);
        int total = (D - B) * (C - A) + (H - F) * (G - E);
        
        return total - shared;
    }
};

 

posted @ 2015-06-09 00:57  卖程序的小歪  阅读(197)  评论(0编辑  收藏  举报