March punch LeetCode activity on day 18 of 836 questions: rectangles overlap (simple)

March punch LeetCode activity on day 18 of 836 questions: rectangles overlap (simple)

  • Title: Rectangular a list [x1, y1, x2, y2] expressed in the form, where (x1, y1) coordinates of the lower left corner, (x2, y2) are the coordinates of the upper right corner. If the intersecting area is positive, called two rectangles overlap. To be clear, the two rectangular sides or only at the corners of the contact does not constitute overlap. Given two rectangles, it determines whether they overlap and returns the result.
    Here Insert Picture Description
  • Problem-solving ideas: take a lot of detours, at the beginning of the situation points too thin. Good abscissa mainly want certain cases do not overlap, the same criteria in other cases.
class Solution {
    public boolean isRectangleOverlap(int[] rec1, int[] rec2) {
       if(rec1[2]<=rec2[0] || rec1[0]>=rec2[2]){
           return false;
       }else{
           if(rec1[3]>rec2[1] && rec1[1]<rec2[3]){
               return true;
           }else{
               return false;
           }
       }
    }
}

Here Insert Picture Description

  • Problem solution practice 1: The idea took me a bit of practice and compression
class Solution {
    public boolean isRectangleOverlap(int[] rec1, int[] rec2) {
        return !(rec1[2] <= rec2[0] ||   // left
                 rec1[3] <= rec2[1] ||   // bottom
                 rec1[0] >= rec2[2] ||   // right
                 rec1[1] >= rec2[3]);    // top
    }
}

作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/rectangle-overlap/solution/ju-xing-zhong-die-by-leetcode-solution/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

Here Insert Picture Description

  • Solution 2 approach problem: a projection approach, a rectangular horizontal and vertical coordinates overlap determines whether the left <right at <a
class Solution {
    public boolean isRectangleOverlap(int[] rec1, int[] rec2) {
        return (Math.min(rec1[2], rec2[2]) > Math.max(rec1[0], rec2[0]) &&
                Math.min(rec1[3], rec2[3]) > Math.max(rec1[1], rec2[1]));
    }
}

作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/rectangle-overlap/solution/ju-xing-zhong-die-by-leetcode-solution/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

Here Insert Picture Description

Published 100 original articles · won praise 12 · views 2350

Guess you like

Origin blog.csdn.net/new_whiter/article/details/104936760