LeetCode刷题实战223:矩形面积
Given the coordinates of two rectilinear rectangles in a 2D plane, return the total area covered by the two rectangles.
示例
解题
class Solution {
public:
int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
long area = (long)(C - A) * (D - B) + (long)(G - E) * (H - F);
long dx = min( C, G ) + 0LL - max( A, E );
if ( dx < 0 ) return area;
long dy = min( D, H ) + 0LL - max( B, F );
if ( dy < 0 ) return area;
return area - dx * dy;
}
};
评论