LeetCode 223. Rectangle Area

来源:互联网 发布:2016软件评测师真题 编辑:程序博客网 时间:2024/04/29 20:34

223. Rectangle Area
My Submissions QuestionEditorial Solution
Total Accepted: 34834 Total Submissions: 117121 Difficulty: Easy
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 maxae = A > E ? A : E;        int mincg = C > G ? G : C;        int maxfb = F > B ? F : B;        int minhd = H > D ? D : H;        int overlap;        if(maxae >= mincg || maxfb >= minhd)            overlap = 0;        else            overlap = (mincg - maxae) * (minhd - maxfb);        return (C - A) * (D - B) + (G - E) * (H - F) - overlap;    }};


0 0
原创粉丝点击