[LeetCode Fortnight Race 23] 3. Are there overlapping circles and rectangles (violence, math, vector, top Solution)

1. Source title

Links: 5361. Are there overlapping circles and rectangles

2. Description title

Here Insert Picture Description
Here Insert Picture Description

3. resolve title

Method One: violence conventional solution +

I direct this question was ignored, I'm sorry my profession ...

Through all points of the rectangle, and the center position of the determination, if the distance between any two returns is less than equal to the radius trueon the line, otherwise falseon the line.

Violence is so no brain, I began to think that this is an infinite number of points on a plane, but not limited integer point, so first of all ruled out violence law, drawing, sample cards have been thinking but, really speechless , I am so sorry my profession. However Violence Act also many defects, if a large amount of data, some TLEof the.

See the code below:

// 执行用时 :124 ms, 在所有 C++ 提交中击败了100.00%的用户
// 内存消耗 :6 MB, 在所有 C++ 提交中击败了100.00%的用户

class Solution {
public:
    bool checkOverlap(int r, int x, int y, int x1, int y1, int x2, int y2) {
        for (int i = x1; i <= x2; ++i) {
            for (int j = y1; j <= y2; ++j) {
                if ((x - i) * (x - i) + (y - j) * (y - j) <= r * r) return true;
            }
        }
        return false;
    }
};

Method Two: Math + clever solution

It's a simple calculation geometry. Gangster borrow solution to a problem area of the picture.
Here Insert Picture Description
They were sentenced to three cases segment on the line. Very clever solution. Here should be noted that C++a plurality of digital with minevaluated function min, you need to add braces, or ambiguous.

class Solution {
public:
    bool checkOverlap(int r, int x, int y, int x1, int y1, int x2, int y2) {
        // 圆心在矩形内部
        if (x >= x1 and x <= x2 and y >= y1 and y <= y2) return true;
        //圆在正方形的 左 右 上 下 时的相交条件
        if ((y >= y1 and y <= y2 and x < x1 and x1 - x <= r) or 
            (y >= y1 and y <= y2 and x > x2 and x - x2 <= r) or 
            (x >= x1 and x <= x2 and y > y2 and y - y2 <= r) or 
            (x >= x1 and (x <= x2 and y < y1 and y1 - y <= r))) return true;
        return min({(x1 - x) * (x1 - x) + (y2 - y) * (y2 - y), (x2 - x) * (x2 - x) + (y2 - y) * (y2 - y), 
                   (x2 - x) * (x2 - x) + (y1 - y) * (y1 - y), (x1 - x) * (x1 - x) + (y1 - y) * (y1 - y)}) <= r * r;
    }
};

Method three: mathematics vector + + + top coordinate transformation solution

Also in solution to a problem region solution learned from known almost big brother Milo Yipshare of almost known. In Gangster know almost explain very clearly, not too elaborate. If doubts can leave a message exchange. This method has a strong scalability.

See the code below:

// 执行用时 :0 ms, 在所有 C++ 提交中击败了100.00%的用户
// 内存消耗 :6 MB, 在所有 C++ 提交中击败了100.00%的用户

class Solution {
public:
    bool checkOverlap(int radius, int x_center, int y_center, int x1, int y1, int x2, int y2) {
        // (tx,ty)表示未变换之前的矩形中心
        int tx = (x1 + x2) / 2, ty = (y1 + y2) / 2; 
        // (tdx,tdy)表示变换之后的矩形中心
        int tdx = -tx, tdy = -ty; 
        // (x11,y11),(x21,y21)表示矩形的左下和右上端点
        int x11 = x1 + tdx, x21 = x2 + tdx, y11 = y1 + tdy, y21 = y2 + tdy;
        // (xc,yc)表示变换到第一象限的圆心坐标 
        int xc = abs(x_center + tdx), yc = abs(y_center + tdy); 
        // (ux,uy)表示u向量
        int ux = xc - x21, uy = yc - y21;
        // 将u向量中的x,y小于0的置为0
        ux = max(0, ux), uy = max(0, uy);
        // 判断
        return ux * ux + uy * uy <= radius * radius;  
    }
};
Published 406 original articles · won praise 368 · views 80000 +

Guess you like

Origin blog.csdn.net/yl_puyu/article/details/105334972