[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.Rectangle Area

2000元阿里云代金券免费领取,2核4G云服务器仅664元/3年,新老用户都有优惠,立即抢购>>>


阿里云采购季(云主机223元/3年)活动入口:请点击进入>>>,


阿里云学生服务器(9.5元/月)购买入口:请点击进入>>>,

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.

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

Credits:
Special thanks to @mithmatt for adding this problem, creating the above image and all test cases.

这个题目重点就是要很精巧的处理好重叠的问题。

public int computeArea(int A, int B, int C, int D, int E, int F, int G,
            int H) {
        int area1 = (C - A) * (D - B);
        int area2 = (G - E) * (H - F);

        int overlapRegion = overlap(A, B, C, D, E, F, G, H);
        return area1 + area2 - overlapRegion;
    }

    private int overlap(int A, int B, int C, int D, int E, int F, int G, int H) {
        int h1 = Math.max(A, E);
        int h2 = Math.min(C, G);
        int h = h2 - h1;

        int v1 = Math.max(B, F);
        int v2 = Math.min(D, H);
        int v = v2 - v1;

        if (h <= 0 || v <= 0)
            return 0;
        else
            return h * v;
    }

别高兴太早哈,这个是不能Accept的,因为在这个测试案例(-1500000001, 0,
-1500000000, 1, 1500000000, 0, 1500000001, 1)
我Debug了一下,是int超出了范围。

public int computeArea(int A, int B, int C, int D, int E, int F, int G,
            int H) {
        int area1 = (C - A) * (D - B);
        int area2 = (G - E) * (H - F);

        int overlapRegion = overlap(A, B, C, D, E, F, G, H);
        return area1 + area2 - overlapRegion;
    }

    private int overlap(int A, int B, int C, int D, int E, int F, int G, int H) {
        // int h1 = Math.max(A, E);
        // int h2 = Math.min(C, G);
        // int h = h2 - h1;
        int h = Math.max(A, E) - Math.min(C, G);

        // int v1 = Math.max(B, F);
        // int v2 = Math.min(D, H);
        // int v = v2 - v1;
        int v = Math.max(B, F) - Math.min(D, H);

        if (h <= 0 || v <= 0)
            return 0;
        else
            return h * v;
    }

还有一种比较好的算法:

public int computeArea2(int A, int B, int C, int D, int E, int F, int G,
            int H) {
        int val = (C - A) * (D - B) + (G - E) * (H - F);
        if (E > C || G < A || F > D || H < B) {
            return val;
        }
        val -= (Math.min(C, G) - Math.max(A, E))
                * (Math.min(D, H) - Math.max(B, F));
        return val;
    }
目录
相关文章
LeetCode 836. 矩形重叠 Rectangle Overlap
LeetCode 836. 矩形重叠 Rectangle Overlap
LeetCode 836. 矩形重叠 Rectangle Overlap
LeetCode 85. Maximal Rectangle
题意是给定一个二维的零一矩阵,1可以用来围成一些矩阵,题意要求是返回围城矩阵的面积最大值.
56 0
LeetCode 85. Maximal Rectangle
Leetcode-Hard 84. Largest Rectangle in Histogram
Leetcode-Hard 84. Largest Rectangle in Histogram
81 0
Leetcode-Hard 84. Largest Rectangle in Histogram
LeetCode之Construct the Rectangle
LeetCode之Construct the Rectangle
61 0
|
Java
[LeetCode]Max Area of Island 岛屿的最大面积
链接:https://leetcode.com/problems/max-area-of-island/description/难度:Easy题目:695.
853 0
LeetCode 223 Rectangle Area(矩形面积)
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/50593348 翻译 找到在二维平面中两个相交矩形的总面积。
795 0
http://www.vxiaotou.com