首页 » 编程之美 » 正文

[leetcode_223]Rectangle Area

计算两个矩形的面积和,有可能有重叠区域。

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 &gt; F ? B : F;</p>

<pre><code>    tx = C &amp;lt; G ? C : G;
    ty = D &amp;lt; H ? D : H;


    int same = 0;
    if (tx &amp;lt;= bx || ty &amp;lt;= by)
    {
        same =  0;
    }
    else
    {
        same = (tx - bx) * (ty - by);
    }

    return (C - A) * (D - B) + (G - E) * (H - F) - same;
}
</code></pre>

<p>};

发表评论