[LeetCode] 836. Rectangle Overlap 矩形重疊

 

A rectangle is represented as a list [x1, y1, x2, y2], where (x1, y1) are the coordinates of its bottom-left corner, and (x2, y2) are the coordinates of its top-right corner.html

Two rectangles overlap if the area of their intersection is positive.  To be clear, two rectangles that only touch at the corner or edges do not overlap.git

Given two (axis-aligned) rectangles, return whether they overlap.github

Example 1:post

Input: rec1 = [0,0,2,2], rec2 = [1,1,3,3]
Output: true

Example 2:url

Input: rec1 = [0,0,1,1], rec2 = [1,0,2,1]
Output: false

Notes:spa

  1. Both rectangles rec1 and rec2 are lists of 4 integers.
  2. All coordinates in rectangles will be between -10^9 and 10^9.

 

這道題讓咱們求兩個矩形是不是重疊,矩形的表示方法是用兩個點,左下和右上點來定位的。下面的講解是參見網友大神jayesch的帖子來的,首先,返璞歸真,在玩 2D 以前,先看下 1D 上是如何運做的。對於兩條線段,它們相交的話能夠是以下狀況:code

           x3             x4
           |--------------|
 |--------------|
 x1             x2

咱們能夠直觀的看出一些關係: htm

x1 < x3 < x2 && x3 < x2 < x4blog

能夠稍微化簡一下:leetcode

x1 < x4 && x3 < x2

就算是調換個位置:

           x1             x2
           |--------------|
 |--------------|
 x3             x4

仍是能獲得一樣的關係:

x3 < x2 && x1 < x4

好,下面咱們進軍 2D 的世界,實際上 2D 的重疊就是兩個方向都同時知足 1D 的重疊條件便可。因爲題目中說明了兩個矩形的重合面積爲正纔算 overlap,就是說挨着邊的不算重疊,那麼兩個矩形重疊主要有這四種狀況:

1)兩個矩形在矩形1的右上角重疊:

           ____________________x4,y4
          |                   |
   _______|______x2,y2        |
  |       |______|____________|
  |      x3,y3   |
  |______________|
 x1,y1

知足的條件爲:x1 < x4 && x3 < x2 && y1 < y4 && y3 < y2

 

2)兩個矩形在矩形1的左上角重疊:

   ___________________  x4,y4
  |                   |
  |            _______|____________x2,y2
  |___________|_______|           |
x3,y3         |                   | 
              |___________________|
            x1,y1

知足的條件爲:x3 < x2 && x1 < x4 && y1 < y4 && y3 < y2

 

3)兩個矩形在矩形1的左下角重疊:

           ____________________x2,y2
          |                   |
   _______|______x4,y4        |
  |       |______|____________|
  |      x1,y1   |
  |______________|
 x3,y3

知足的條件爲:x3 < x2 && x1 < x4 && y3 < y2 && y1 < y4

 

4)兩個矩形在矩形1的右下角重疊:

   ___________________  x2,y2
  |                   |
  |            _______|____________x4,y4
  |___________|_______|           |
x1,y1         |                   | 
              |___________________|
            x3,y3

知足的條件爲:x1 < x4 && x3 < x2 && y3 < y2 && y1 < y4

仔細觀察能夠發現,上面四種狀況的知足條件其實都是相同的,只不過順序調換了位置,因此咱們只要一行就能夠解決問題了,碉堡了。。。

 

class Solution {
public:
    bool isRectangleOverlap(vector<int>& rec1, vector<int>& rec2) {
        return rec1[0] < rec2[2] && rec2[0] < rec1[2] && rec1[1] < rec2[3] && rec2[1] < rec1[3];     
    }
};

 

Github 同步地址:

https://github.com/grandyang/leetcode/issues/836

 

相似題目:

Rectangle Area

 

參考資料:

https://leetcode.com/problems/rectangle-overlap/

https://leetcode.com/problems/rectangle-overlap/discuss/132319/My-One-Line-C%2B%2B-Solution

https://leetcode.com/problems/rectangle-overlap/discuss/133175/C%2B%2B-Solution-with-easy-explanation

 

LeetCode All in One 題目講解彙總(持續更新中...)

相關文章
相關標籤/搜索