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.

本题是一道简单的数学题,意思就是要求两个矩形的覆盖面积。

根据每个矩形是由它的下左边角和它的上右边角定义的特征,再结合公式:覆盖面积=两个矩形的面积-相交的面积,即可。

代码如下:

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);
        if (A >= G || B >= H || C <= E || D <= F)
        {
        return area;
        }
        int top = (D>H)?H:D; //和用min(D,H)是一样的
        int bottom = max(B, F);
        int left = max(A, E);
        int right = min(C, G);
        return area - (top-bottom)*(right-left);
    }
};

看了看别人做的,

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

    if(A > E) return computeArea(E, F, G, H, A, B, C, D);

    int res = (C - A)*(D- B) + (G - E)*(H - F);

    if(C > E && B < H && F < D) res -= (min(C, G) - E) * (min(D, H) - max(B, F));

    return res;

}
};

核心思想都是差不多的。 

  

时间: 2024-10-27 07:10:22

leetcode:Rectangle Area的相关文章

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] 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

223 -- 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

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

leetCode(33):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. cl

Java for LeetCode 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 possible value of int. Cr

LeetCode 850. Rectangle Area II

经典line sweep问题,和 perfect rectangle 很类似,但是要考虑多个矩形左边界一样的情况,加个id来区分. 和另一道类似的题 the skyline problem 不同的是,没有很多 corner cases,对 x 排序非常简单. 难点是在 active set 里,和 merge intervals 那题一样,计算 total_y,乘以两个相邻 event 的 delta_x. class Solution { public: struct interval { i

leetcode 223. Rectangle Area 计算面积---------- java

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. 计算