Rectangle Area (M)
題目
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.

Example:
Input: A = -3, B = 0, C = 3, D = 4, E = 0, F = -1, G = 9, H = 2
Output: 45
Note:
Assume that the total area is never beyond the maximum possible value of int.
題意
計算兩個矩形一共占據的面積,
思路
所求結果為總面積和減去重疊部分面積,
代碼實作
Java
class Solution {
public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
int S = (C - A) * (D - B) + (G - E) * (H - F);
if (C >= E && G >= A && D >= F && H >= B) {
int length = (C - A + G - E) - (Math.max(C, G) - Math.min(A, E));
int width = (D - B + H - F) - (Math.max(D, H) - Math.min(B, F));
S -= length * width;
}
return S;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/38871.html
標籤:其他
上一篇:是不是家用路由器不支持在無線下進行組播multicast
下一篇:ospf isis rip 異同
