[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

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 value of int.

解题思路

面积 = 总面积 - 重叠面积

实现代码

// Runtime: 2 ms
class Solution {
public:
    int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
        int area = (D - B) * (C - A) + (H - F) * (G - E);
        if (A >= G || B >= H || C <= E || D <= F)
        {
            return area;
        }

        int top = min(D, H);
        int right = min(C, G);
        int bottom = max(B, F);
        int left = max(A, E);

        return area - (top - bottom) * (right - left);
    }
};
目录
相关文章
LeetCode 836. 矩形重叠 Rectangle Overlap
LeetCode 836. 矩形重叠 Rectangle Overlap
LeetCode 836. 矩形重叠 Rectangle Overlap
LeetCode 85. Maximal Rectangle
题意是给定一个二维的零一矩阵,1可以用来围成一些矩阵,题意要求是返回围城矩阵的面积最大值.
83 0
LeetCode 85. Maximal Rectangle
Leetcode-Hard 84. Largest Rectangle in Histogram
Leetcode-Hard 84. Largest Rectangle in Histogram
103 0
Leetcode-Hard 84. Largest Rectangle in Histogram
LeetCode之Construct the Rectangle
LeetCode之Construct the Rectangle
77 0
|
Java
[LeetCode]Max Area of Island 岛屿的最大面积
链接:https://leetcode.com/problems/max-area-of-island/description/难度:Easy题目:695.
873 0
LeetCode 223 Rectangle Area(矩形面积)
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/50593348 翻译 找到在二维平面中两个相交矩形的总面积。
821 0