Rectangle Area

update 2018-10-21 02:17:29

LeetCodearrow-up-right

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:

Note:

Assume that the total area is never beyond the maximum possible value of int.

Basic Idea:

我们可以根据图来理解。求两个矩形组成图形的总面积就是两矩形面积之和减去重叠部分面积。所以关键就是如何求重叠部分的面积,可以如图观察发现,底边长等于两右边界中最小值减去两左边界中最大值。而高则等于两上边界最小值减去两下边界最大值。

Java Code: