zoukankan      html  css  js  c++  java
  • [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 flsak 框架
    postman
    压力测试和负载测试
    软件测试相关内容
    Linux常用基本命令
    leetcode刷题——反转字符串
    leetcode——搜索插入位置
    leetcode刷题——移除元素
    leetcode 刷题 ——删除排序数组中的重复项
    json.decoder.JSONDecodeError: Expecting value 错误的处理
  • 原文地址:https://www.cnblogs.com/lz87/p/6951549.html
Copyright © 2011-2022 走看看