[Swift]LeetCode836. 矩形重疊 | Rectangle Overlap

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: http://www.javashuo.com/article/p-wyqueora-me.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

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.git

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.github

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

Example 1:spa

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

Example 2:code

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

Notes:htm

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

矩形以列表 [x1, y1, x2, y2] 的形式表示,其中 (x1, y1) 爲左下角的座標,(x2, y2)是右上角的座標。blog

若是相交的面積爲正,則稱兩矩形重疊。須要明確的是,只在角或邊接觸的兩個矩形不構成重疊。get

給出兩個矩形,判斷它們是否重疊並返回結果。博客

示例 1:

輸入:rec1 = [0,0,2,2], rec2 = [1,1,3,3]
輸出:true

示例 2:

輸入:rec1 = [0,0,1,1], rec2 = [1,0,2,1]
輸出:false

說明:

  1. 兩個矩形 rec1 和 rec2 都以含有四個整數的列表的形式給出。
  2. 矩形中的全部座標都處於 -10^9 和 10^9 之間。

Runtime: 4 ms
Memory Usage: 19.2 MB
1 class Solution {
2     func isRectangleOverlap(_ rec1: [Int], _ rec2: [Int]) -> Bool {
3         return rec1[0] < rec2[2] && rec2[0] < rec1[2] && rec1[1] < rec2[3] && rec2[1] < rec1[3]
4     }
5 }

8ms

1 class Solution {
2     func isRectangleOverlap(_ rec1: [Int], _ rec2: [Int]) -> Bool {        
3         return (min(rec1[2], rec2[2]) > max(rec1[0], rec2[0])) && 
4         (min(rec1[3], rec2[3]) > max(rec1[1], rec2[1]))
5     }
6 }
相關文章
相關標籤/搜索