• [LeetCode 836] Rectangle Overlap


    Given two rectangles, find if the given two rectangles overlap or not.

     Notice

    l1: Top Left coordinate of first rectangle.
    r1: Bottom Right coordinate of first rectangle.
    l2: Top Left coordinate of second rectangle.
    r2: Bottom Right coordinate of second rectangle.

    l1 != r2 and l2 != r2

    Example

    Given l1 = [0, 8], r1 = [8, 0], l2 = [6, 6], r2 = [10, 0], return true

    Given l1 = [0, 8], r1 = [8, 0], l2 = [9, 6], r2 = [10, 0], return false

     

    The solution itself does not worth noticing too much as it is really easy. The take away of this problem is when trying to check if a condition is true or false, we can do it in two ways. Either check if all the true conditions are met; Or check if all the false conditions are met. 

     

    For this problem, checking if the two given rectangles overlap directly is complicated. So we check if they do not overlap, then return the reversed check result.

     

     1 /**
     2  * Definition for a point.
     3  * class Point {
     4  *     public int x, y;
     5  *     public Point() { x = 0; y = 0; }
     6  *     public Point(int a, int b) { x = a; y = b; }
     7  * }
     8  */
     9 
    10 public class Solution {
    11     /**
    12      * @param l1 top-left coordinate of first rectangle
    13      * @param r1 bottom-right coordinate of first rectangle
    14      * @param l2 top-left coordinate of second rectangle
    15      * @param r2 bottom-right coordinate of second rectangle
    16      * @return true if they are overlap or false
    17      */
    18     public boolean doOverlap(Point l1, Point r1, Point l2, Point r2) {
    19         // Write your code here
    20         return !(l1.x > r2.x || r1.x < l2.x || r1.y > l2.y || l1.y < r2.y);
    21     }
    22 }

     

    Related Problems

    [LeetCode 223] Rectangle Area
    [LeetCode 1401] Circle and Rectangle Overlapping

  • 相关阅读:
    获取当前时区时间
    python lambda表达式详解
    Odoo 12开发之开发环境准备
    初步了解odoo12
    web前端面试题
    实现一个优先级队列
    面试题
    python读取和生成excel文件
    Django基础
    virtualenv
  • 原文地址:https://www.cnblogs.com/lz87/p/6951549.html
Copyright © 2020-2023  润新知