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

  • 相关阅读:
    hadoop节点的增删
    hadoop集群搭建
    主从配置
    CentOS7ssh互信
    Java根据视频的URL地址,获取视频时长
    Mybatis-plus使用@Select注解使用IN查询不出数据的问题
    洗牌算法
    1525
    SpringBoot+Quartz+MySQL实现分布式定时任务
    微信小程序授权登录解密失败问题
  • 原文地址:https://www.cnblogs.com/lz87/p/6951549.html
Copyright © 2011-2022 走看看