Rectangle Area - LeetCode

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.

思路:问题的关键点是判断是否两个矩形存在覆盖。

若存在覆盖时,必定有:A和E的最大值要小于C和G的最小值;B和F的最大值要小于D和H的最小值。

因此我们有如下代码

1 class Solution {
2 public:
3     int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
4         int left = max(A, E), right = max(min(C, G), left);
5         int bot = max(B, F), top = max(min(D, H), bot);
6         return (C - A) * (D - B) + (G - E) * (H - F)
7             - (right - left) * (top - bot);
8     }
9 };

其中减去的部分就是重叠的面积,当不重叠时该值为0。

时间: 2024-10-25 22:57:48

Rectangle Area - LeetCode的相关文章

Rectangle Area——LeetCode

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. 题目

LeetCode:Rectangle Area - 矩形交叉部分的面积

1.题目名称 Rectangle Area(矩形交叉部分的面积) 2.题目地址 https://leetcode.com/problems/rectangle-area/ 3.题目内容 英文: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

LeetCode 223. 矩形面积(Rectangle Area)

223. 矩形面积 223. Rectangle Area 题目描述 在二维平面上计算出两个由直线构成的矩形重叠后形成的总面积. 每个矩形由其左下顶点和右上顶点坐标表示,如图所示. LeetCode223. Rectangle Area中等 示例: 输入: -3, 0, 3, 4, 0, -1, 9, 2 输出: 45 说明: 假设矩形面积不会超出 int 的范围. Java 实现 class Solution { public int computeArea(int A, int B, int

[LeetCode] 850. Rectangle Area II 矩形面积之二

We are given a list of (axis-aligned)?rectangles.? Each?rectangle[i] = [x1, y1, x2, y2]?, where (x1, y1) are the coordinates of the bottom-left corner, and (x2, y2) are the coordinates of the top-right corner of the?ith rectangle. Find the total area

【leetcode】Contains Duplicate & Rectangle Area(easy)

Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct. 思路:简单题用set bool cont

LeetCode223: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. Rectangle Area Assume that the total area is never beyond the maximum possible v

No.223 Rectangle Area

No.223 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 pos

leetcode之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. 这道

【Leetcode】Rectangle Area

题目链接: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 are