Python Leetcode(836.矩形重叠)
2019-04-29 15:07
411 查看
Python Leetcode(836.矩形重叠)
矩形以列表 [x1, y1, x2, y2] 的形式表示,其中 (x1, y1) 为左下角的坐标,(x2, y2) 是右上角的坐标。
如果相交的面积为正,则称两矩形重叠。需要明确的是,只在角或边接触的两个矩形不构成重叠。
给出两个矩形,判断它们是否重叠并返回结果。
示例 1:
输入:rec1 = [0,0,2,2], rec2 = [1,1,3,3]
输出:true
示例 2:
输入:rec1 = [0,0,1,1], rec2 = [1,0,2,1]
输出:false
说明:
两个矩形 rec1 和 rec2 都以含有四个整数的列表的形式给出。 矩形中的所有坐标都处于 -10^9 和 10^9 之间。
Solution:(最开始想的是其中一个矩形的点是否能落到另一个较大的矩形中,发现数字大了之后,数字会溢出。看了解析之后的思路是:计算它们的坐标的距离,若x方向有相交,则(x4-x1)(x2-x3)应该大于0,y轴同理。)
class Solution(object): def isRectangleOverlap(self, rec1, rec2): """ :type rec1: List[int] :type rec2: List[int] :rtype: bool """ x1, y1, x2, y2 = rec1 x3, y3, x4, y4 = rec2 return (x4 - x1) * (x2 - x3) > 0 and (y4 - y1) * (y2 - y3) > 0
solution = Solution() print(solution.isRectangleOverlap([0,0,2,2], [1,1,3,3]))
True
相关文章推荐
- [LeetCode] Random Point in Non-overlapping Rectangles 非重叠矩形中的随机点
- 利用python 完成leetcode85 最大矩形
- 平面中任意方位矩形的重叠面积——python
- Leetcode 6. ZigZag Conversion The Solution of Python
- LeetCode 65. Valid Number--Python实现
- LeetCode题解(python)-24. 两两交换链表中的节点
- LeetCode--Edit Distance(字符串编辑距离)Python
- 斐波那契数列和应用举例我们可以用2*1的小矩形横着或者竖着去覆盖更大的矩形。请问用n个2*1的小矩形无重叠地覆盖一个2*n的大矩形,总共有多少种方法?
- leetcode 【 Rotate Image 】python 实现
- leetcode 日经贴,python code -Longest Valid Parentheses
- LeetCode 148. Sort List 单链表排序 Python Solution
- [算法Rust,Go,Python,JS实现)]LeetCode之27-移除元素
- 利用python 完成 leetcode132 分割回文串 II
- leetcode(11),Power of Three(python)
- leetcode 867. 转置矩阵(python)
- leetcode223题 题解 翻译 C语言版 Python版
- [python]Leetcode2-Add Two Numbers
- [leetcode: Python]461. Hamming Distance
- leetcode3题 题解 翻译 C语言版 Python版
- [leetcode, python] Reverse Words in a String 反转字符串