题目描述
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.
Example:
1 | Input: A = -3, B = 0, C = 3, D = 4, E = 0, F = -1, G = 9, H = 2 |
Note:
Assume that the total area is never beyond the maximum possible value of int.
这题的核心就是求重叠部分的面积。
实际上,重叠部分的左下角为(max(A, E), max(B, F)),右上角为(min(C, G), min(D, H)),如果左下角的点不在右上角的点的左下方,则重叠面积为0。
代码实现
1 | class Solution: |