# [Leetcode] 223. Rectangle Area 解题报告

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.

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

1、自己的代码：

class Solution {
public:
int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
int area1 = (C - A) * (D - B);
int area2 = (G - E) * (H - F);
if (E >= C || G <= A || H <= B || F >= D) {     // no overlap
return area1 + area2;
}
int width = 0, height = 0;
if (E < A) {                                    // update width
width = G < C ? (G - A) : (C - A);
}
else {
width = G < C ? (G - E) : (C - E);
}
if (F < B) {                                    // update height
height = H < D ? (H - B) : (D - B);
}
else {
height = H < D ? (H - F) : (D - F);
}
return area1 + area2 - width * height;
}
};

2、别人的代码：

class Solution {
public:
int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
int area = (C - A) * (D - B) + (G - E) * (H - F);
if(!(B >= H || F >= D || A >= G ||E >= C)) {
int tem = (min(C, G) - max(A, E)) * (min(D, H) -max(B, F));
area -= tem;
}
return area;
}
};
class Solution {
public:
int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
int left = max(A, E), right = max(min(C, G), left);
int bot = max(B, F), top = max(min(D, H), bot);
return (C - A) * (D - B) + (G - E) * (H - F) - (right - left) * (top - bot);
}
};

#### Leetcode_223_Rectangle Area

2015-07-13 22:02:28

#### Leetcode 223 Rectangle Area 矩形区域面积

2015-08-28 17:51:41

#### 【LeetCode-面试算法经典-Java实现】【223-Rectangle Area（矩形区域）】

2015-08-29 06:39:48

#### LeetCode223——Rectangle Area

2015-07-31 17:44:24

#### [leetcode] 223. Rectangle Area 解题报告

2015-12-28 15:02:48

#### 223. Rectangle Area LeetCode

2016-02-12 12:34:51

#### [LeetCode]--223. Rectangle Area

2016-10-11 23:02:06

#### Leetcode 223. Rectangle Area

2016-09-13 03:26:25

#### LeetCode : 223. Rectangle Area

2016-08-30 16:02:19