首页 > 其他分享 >leetcode 836. Rectangle Overlap

leetcode 836. Rectangle Overlap

时间:2023-05-30 17:33:05浏览次数:49  
标签:836 int type Overlap overlap rec2 rec1 rectangles leetcode

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.

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.

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

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

Notes:

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

解法1:

class Solution(object):
    def isRectangleOverlap(self, rec1, rec2):
        """
        :type rec1: List[int]
        :type rec2: List[int]
        :rtype: bool
        """
        return not (rec1[1] >= rec2[3] # higher
                or rec1[3] <= rec2[1] # lower
                or rec1[2] <= rec2[0] # lefter
                or rec1[0] >= rec2[2]) # righter

解法2:

class Solution:
    def isRectangleOverlap(self, rec1, rec2):
        """
        :type rec1: List[int]
        :type rec2: List[int]
        :rtype: bool
        """
        x_overlap = min(rec1[2], rec2[2]) - max(rec1[0], rec2[0])
        y_overlap = min(rec1[3], rec2[3]) - max(rec1[1], rec2[1])
        return x_overlap > 0 and y_overlap > 0

 

标签:836,int,type,Overlap,overlap,rec2,rec1,rectangles,leetcode
From: https://blog.51cto.com/u_11908275/6381023

相关文章

  • leetcode 342. Power of Four
    Givenaninteger(signed32bits),writeafunctiontocheckwhetheritisapowerof4.Example:Givennum=16,returntrue.Givennum=5,returnfalse.Followup:Couldyousolveitwithoutloops/recursion?解法1,经典的数学解法:classSolution(object):def......
  • leetcode 345. Reverse Vowels of a String
    Writeafunctionthattakesastringasinputandreverseonlythevowelsofastring.Example1:Givens="hello",return"holle".Example2:Givens="leetcode",return"leotcede".Note:Thevowelsdoesnotincl......
  • leetcode Most Common Word——就是在考察自己实现split
    819.MostCommonWordGivenaparagraph andalistofbannedwords,returnthemostfrequentwordthatisnotinthelistofbannedwords. Itisguaranteedthereisatleastonewordthatisn'tbanned,andthattheanswerisunique.Wordsinthelist......
  • leetcode Kth Largest Element in a Stream——要熟悉heapq使用
    703.KthLargestElementinaStreamEasyDesignaclasstofind thekthlargestelementinastream.Notethatitisthekthlargestelementinthesortedorder,notthekthdistinctelement.Your KthLargest classwillhaveaconstructorwhichacceptsanin......
  • leetcode 2712. 使所有字符相等的最小成本
    2712.使所有字符相等的最小成本给你一个下标从 0 开始、长度为 n 的二进制字符串 s ,你可以对其执行两种操作:选中一个下标 i 并且反转从下标 0 到下标 i(包括下标 0 和下标 i )的所有字符,成本为 i+1 。选中一个下标 i 并且反转从下标 i 到下标 n-......
  • leetcode 2707. 字符串中的额外字符
    2707.字符串中的额外字符给你一个下标从 0 开始的字符串 s 和一个单词字典 dictionary 。你需要将 s 分割成若干个 互不重叠 的子字符串,每个子字符串都在 dictionary 中出现过。s 中可能会有一些 额外的字符 不在任何子字符串中。请你采取最优策略分割 s......
  • [LeetCode] 51. N-Queens
    The n-queens puzzleistheproblemofplacing n queensonan nxn chessboardsuchthatnotwoqueensattackeachother.Givenaninteger n,return alldistinctsolutionstothe n-queenspuzzle.Youmayreturntheanswerin anyorder.Eachsolution......
  • #yyds干货盘点# LeetCode程序员面试金典:填充每个节点的下一个右侧节点指针 II
    题目:给定一个二叉树:structNode{ intval; Node*left; Node*right; Node*next;}填充它的每个next指针,让这个指针指向其下一个右侧节点。如果找不到下一个右侧节点,则将next指针设置为NULL。初始状态下,所有 next指针都被设置为NULL。 示例1:输入:root=[1,2,3......
  • 用自然语言让AI打leetcode周赛
    还在自己吭哧吭哧打算法平台Leetcode的周赛?为什么不试试神奇的ChatGPT类AI呢!用AI助手Claude参加第103场周赛,共四道题,均完成了AC,能达到参与者前10%的成绩。事情的起因是知乎上一位叫萧雅的用户尝试使用AI进行编程,但在测试过程中,她发现直接给出题目让AI进行编程并输出结果的方法,效......
  • LeetCode 530. 二叉搜索树的最小绝对差
    题目链接:LeetCode530.二叉搜索树的最小绝对差题意:给你一个二叉搜索树的根节点root,返回树中任意两不同节点值之间的最小差值。差值是一个正数,其数值等于两值之差的绝对值。解题思路:递归法1既然是二叉搜索树,那么中序遍历一定是有序的,因此最小的插值一定出现在相邻的两个......