【一天一道LeetCode】#223. Rectangle Area

一天一道LeetCode

本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github

欢迎大家关注我的新浪微博,我的新浪微博

欢迎转载,转载请注明出处

(一)题目

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.

【一天一道LeetCode】#223. Rectangle Area

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

(二)解题

题目大意:求两个矩形总的覆盖面积。

解题思路:判断两个数组不重叠,满足C<=E||G<=A||F>=D||B>=H,

如果重叠求重叠面积:(min(C,G)-max(A,E))*(min(D,H)-max(B,F))。

下面是代码:

class Solution {
public:
    int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
        int total = (C-A)*(D-B)+(G-E)*(H-F);
        if(C<=E||G<=A||F>=D||B>=H) return total;//不重叠
        return total - (min(C,G)-max(A,E))*(min(D,H)-max(B,F));//重叠
    }
};
上一篇:关于概率dp的HINT


下一篇:前端mv框架下(目前写的是vue),对组件抽象的思考