计算两个矩形的面积和,有可能有重叠区域。
class Solution { public: int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) { int bx,by,tx,ty; bx = A > E ? A : E; by = B > F ? B : F;</p> <pre><code> tx = C &lt; G ? C : G; ty = D &lt; H ? D : H; int same = 0; if (tx &lt;= bx || ty &lt;= by) { same = 0; } else { same = (tx - bx) * (ty - by); } return (C - A) * (D - B) + (G - E) * (H - F) - same; } </code></pre> <p>};