• Rectangle Area


    柿子专挑软的捏,从最简单的题目开始练,现在上题目:

    223. Rectangle Area

    • Total Accepted: 42894
    • Total Submissions: 139528
    • Difficulty: Easy

    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.

    Rectangle Area

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

    题目的意思是如果两个矩形不相交,则总的面积为两个矩形的面积和,如果相交,则用两个矩形的面积之和减去相交的面积;

    智商太菜,这么简单的题目,我都要去百度别人的思路,主要体现在相交时候,数学太菜。上代码:

    class Solution {
    public:
    int computeArea(int A, int B, int C, int D, int E, int F, int G, int H)
    {
    int areasum = ((C - A)*(D - B)) + ((G - E)*(H - F));

    int a1 = min(A, C);
    int a2 = max(E, G);
    int a3 = max(A, C);
    int a4 = min(E, G);

    int a5 = min(B, D);
    int a6 = max(F, H);
    int a7 = max(B, D);
    int a8 = min(F, H);
    if ((a1 >= a2) || (a3 <= a4))//不相交情况
    {
    return areasum;
    }

    if ((a5 >= a6) || (a7 <= a8))//不相交情况
    {
    return areasum;
    }

    //以下就是相交的情况
    int right = min(C, G);
    int left = max(A, E);
    int top = min(H, D);
    int bottom = max(F, B);
    int commonArea = (right - left) * (top - bottom);

    return areasum - commonArea;
    }
    };

    太笨了,我要编程编到死!fuck!

  • 相关阅读:
    整数反转
    最长公共前缀
    罗马数字转整数
    单点登录
    VMware Workstation虚拟机密钥
    Pytest 用例内部执行顺序
    判断是不是回文数
    python端口IP字符串是否合法
    python求二叉树深度
    有两个字符串类型的数字,实现一个方法将它们进行相加,并返回相加后的数值。
  • 原文地址:https://www.cnblogs.com/a-dreaming-dreamer/p/5710979.html
Copyright © 2020-2023  润新知