From : https://leetcode.com/problems/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.

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 area = (C-A)*(D-B) + (G-E)*(H-F);
int top = min(D, H);
int bottom = max(B, F);
int left = max(A, E);
int right = min(C, G);
return area - (top>bottom && left<right)*(top-bottom)*(right-left);
}
};
本文介绍了一个计算两个矩形在二维平面上覆盖总面积的方法,通过定义类和函数来实现计算过程,确保总面积不会超出整数的最大可能值。
4万+

被折叠的 条评论
为什么被折叠?



