leetcode---Rectangle Area

来源:互联网 发布:八进制转十进制算法 编辑:程序博客网 时间:2024/06/01 08:45

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 a = max(A, E), b = max(B, F);  //左下        int c = min(C, G), d = min(D, H);  //右上        int s1 = (C-A) * (D-B);        int s2 = (G-E) * (H-F);        if(d <= b || c <= a)            return s1 + s2;        else            return s1 + s2 - (c-a) * (d-b);    }};
0 0
原创粉丝点击