LeetCode Rectangle Area

LeetCode Rectangle Area

题目

这里写图片描述

思路

刚开始自己写别提WA多少遍了;
后来看到标达真的被惊讶到了;
代码可以这么美;

代码

自己的

#define min(A, B) (A > B ? B : A)

bool pointInRectangle(int px, int py, int ax, int ay, int bx, int by) {
    return ax <= px && px <= bx && ay <= py && py <= by;
}

int computeAreaSub(int A, int B, int C, int D, int E, int F, int G, int H) {
    int area1 = (D - B) * (C - A), area2 = (H - F) * (G - E);
    if (pointInRectangle(A, B, E, F, G, H)      // left down
        && pointInRectangle(A, D, E, F, G, H)      // left up
        && pointInRectangle(C, D, E, F, G, H)      // right up
        && pointInRectangle(C, B, E, F, G, H)) {   // right down
        return area2;
    }
    if (pointInRectangle(A, B, E, F, G, H)      // left down
        && pointInRectangle(A, D, E, F, G, H)      // left up
        && !pointInRectangle(C, D, E, F, G, H)      // right up
        && !pointInRectangle(C, B, E, F, G, H)) {   // right down
        return area1 + area2 - (D - B) * (G - A);
    }
    if (!pointInRectangle(A, B, E, F, G, H)      // left down
        && !pointInRectangle(A, D, E, F, G, H)      // left up
        && pointInRectangle(C, D, E, F, G, H)      // right up
        && pointInRectangle(C, B, E, F, G, H)) {   // right down
        return area1 + area2 - (D - B) * (C - E);
    }
    if (!pointInRectangle(A, B, E, F, G, H)      // left down
        && pointInRectangle(A, D, E, F, G, H)      // left up
        && pointInRectangle(C, D, E, F, G, H)      // right up
        && !pointInRectangle(C, B, E, F, G, H)) {   // right down
        return area1 + area2 - (D - F) * (C - A);
    }
    if (pointInRectangle(A, B, E, F, G, H)      // left down
        && !pointInRectangle(A, D, E, F, G, H)      // left up
        && !pointInRectangle(C, D, E, F, G, H)      // right up
        && pointInRectangle(C, B, E, F, G, H)) {   // right down
        return area1 + area2 - (H - B) * (C - A);
    }
    if (pointInRectangle(A, B, E, F, G, H)      // left down
        && !pointInRectangle(A, D, E, F, G, H)      // left up
        && !pointInRectangle(C, D, E, F, G, H)      // right up
        && !pointInRectangle(C, B, E, F, G, H)) {   // right down
        return area1 + area2 - (G - A) * (H - B);
    }
    if (!pointInRectangle(A, B, E, F, G, H)      // left down
        && !pointInRectangle(A, D, E, F, G, H)      // left up
        && pointInRectangle(C, D, E, F, G, H)      // right up
        && !pointInRectangle(C, B, E, F, G, H)) {   // right down
        return area1 + area2 - (C - E) * (D - F);
    }
    if (!pointInRectangle(A, B, E, F, G, H)      // left down
        && pointInRectangle(A, D, E, F, G, H)      // left up
        && !pointInRectangle(C, D, E, F, G, H)      // right up
        && !pointInRectangle(C, B, E, F, G, H)) {   // right down
        return area1 + area2 - (D - F) * (G - A);
    }
    if (!pointInRectangle(A, B, E, F, G, H)      // left down
        && !pointInRectangle(A, D, E, F, G, H)      // left up
        && !pointInRectangle(C, D, E, F, G, H)      // right up
        && pointInRectangle(C, B, E, F, G, H)) {   // right down
        return area1 + area2 - (H - B) * (C - E);
    }
    return area1 + area2;
}

int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
    int temp1 = computeAreaSub(A, B, C, D, E, F, G, H);
    int temp2 = computeAreaSub(E, F, G, H, A, B, C, D);
    return min(computeAreaSub(A, B, C, D, E, F, G, H), computeAreaSub(E, F, G, H, A, B, C, D));
}

Discuss的

#define min(A, B) (A > B ? B : A)
#define max(A, B) (A > B ? A : B)

int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
    int a = max(A, E), b = max(B, F), c = min(C, G), d = min(D, H);
    return (C - A) * (D - B) + (G - E) * (H - F) - ((a < c && b < d) ? (c - a) * (d - b) : 0);
}
阅读更多
文章标签: rectangle area
个人分类: leetcode
上一篇LeetCode Count Complete Tree Nodes
下一篇LeetCode Invert Binary Tree
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭