【LeetCode】223

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.

Solution: 两长方形面积之和减去重合部分面积;当A>=G或C<=E或B>=H或D<=F时,两长方形不相交;

 1 class Solution {
 2 public:
 3     int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
 4         int width,height;
 5         if(A>=G||C<=E||B>=H||D<=F){
 6             width=0;
 7             height=0;
 8         }else{
 9             if(A<=E)
10                 width=min(C-E,G-E);
11             else
12                 width=min(C-A,G-A);
13                 
14             if(B<=F)
15                 height=min(D-F,H-F);
16             else
17                 height=min(D-B,H-B);
18         }
19         return (C-A)*(D-B)+(G-E)*(H-F)-height*width;
20     }
21 };
原文地址:https://www.cnblogs.com/irun/p/4696629.html