LeetCode:矩形区域【223】

时间:2022-07-24 16:00:36

LeetCode:矩形区域【223】

题目描述

在二维平面上计算出两个由直线构成的矩形重叠后形成的总面积。

每个矩形由其左下顶点和右上顶点坐标表示,如图所示。

LeetCode:矩形区域【223】

示例:

输入: -3, 0, 3, 4, 0, -1, 9, 2 输出: 45

说明: 假设矩形面积不会超出 int 的范围。

题目分析

这道题目应该很简单,但是思路一定清晰,即搞清楚矩形重叠的条件

LeetCode:矩形区域【223】

Java题解

public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {

        int areaOfSqrA = (C-A) * (D-B);
int areaOfSqrB = (G-E) * (H-F); int left = Math.max(A, E);
int right = Math.min(G, C);
int bottom = Math.max(F, B);
int top = Math.min(D, H); //If overlap
int overlap = 0;
if(right > left && top > bottom)
overlap = (right - left) * (top - bottom); return areaOfSqrA + areaOfSqrB - overlap;
}