leetcode 836. Rectangle Overlap rectangles overlap
leetcode 2020 March 1 question daily punch
this question in360 Spring recruit and AliHave encountered
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.
Example 1:
Input: rec1 = [0,0,2,2], rec2 = [1,1,3,3]
Output: true
Example 2:
Input: rec1 = [0,0,1,1], rec2 = [1,0,2,1]
output: false
Tip:
two rectangular rec1 rec2 and are given in the form of a list containing four integers.
All coordinates in the rectangle are between 9 and 10 ^ -10 ^ 9.
default x-axis points to the right, y-axis pointing to the default.
You can consider only rectangular case is being put.
Ideas: Python Math question: length and width are respectively intersect
Code:
class Solution(object):
def isRectangleOverlap(self, rec1, rec2):
"""
:type rec1: List[int]
:type rec2: List[int]
:rtype: bool
"""
# 长和宽都分别相交
h1=rec1[2]-rec1[0] #rec1的长
v1=rec1[3]-rec1[1] #rec1的宽
h2=rec2[2]-rec2[0] #rec2的长
v2=rec2[3]-rec2[1] #rec2的宽
hflag=False
vflag=False
if rec1[0]<=rec2[0]:#rec1在rec2左面
if rec2[0]-rec1[0]<h1:
hflag=True
else:#rec1在rec2右面
if rec1[0]-rec2[0]<h2:
hflag=True
if rec1[1]<=rec2[1]:#rec1在rec2下面
if rec2[1]-rec1[1]<v1:
vflag=True
else:#rec1在rec2上面
if rec1[1]-rec2[1]<v2:
vflag=True
return hflag and vflag
This blog is an original work, welcomed the guidance, reproduced, please indicate the source, attach a link to this article, thank you!