LeetCode 223. Rectangle Area

来源:互联网 发布:latex软件 编辑:程序博客网 时间:2024/05/16 07:47

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.

本来写的分类讨论,太麻烦,最后参考的网上的答案。数学太差。

class Solution {public:    int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {        int l1 = max(A, E);        int l2 = max(B, F);        int r1 = min(C, G);        int r2 = min(D, H);        int num = (r1 - l1) * (r2 - l2);        if(l1 >= r1 || l2 >= r2) num = 0;        return (C - A) * (D - B) + (G - E) * (H - F) - num;    }};


0 0
原创粉丝点击