题目:

Find the total area covered by two rectilinear rectangles in a 2D plane.

Each rectangle is defined by its bottom left corner and top right corner as shown in the figure.

Assume that the total area is never beyond the maximum possible value of int.

链接: http://leetcode.com/problems/rectangle-area/

题解:

数学题,需要判断矩阵是否相交,相交的话减去重复面积(顶点相交除外)。

Time Complexity - O(1), Space Complexity - O(1)。

public class Solution {
public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
int area = (C - A) * (D - B) + (G - E) * (H - F);
if(A >= G || B >= H || C <= E || D <= F)
return area; int duplicate = (Math.min(C, G) - Math.max(A, E)) * (Math.min(D, H) - Math.max(B, F));
return area - duplicate;
}
}

二刷:

方法跟一刷一样

Java:

Time Complexity - O(1), Space Complexity - O(1)。

public class Solution {
public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
int totalArea = (C - A) * (D - B) + (G - E) * (H - F);
if (A >= G || B >= H || C <= E || D <= F) {
return totalArea;
}
int sameArea = (Math.min(C, G) - Math.max(A, E)) * (Math.min(D, H) - Math.max(B, F));
return totalArea - sameArea;
}
}

三刷:

Java:

  1. 一开始先计算出两个矩形的面积和 totalArea
  2. 判断两个矩形是否相交,假如不相交,或者仅有顶点相交,那么我们直接返回totalArea。 这里两个矩形 x 的范围是 (A, C), (E, F),  y的范围是(B, D), (E, F)
  3. 计算overlap的面积,边的计算公式是 ( 最小的上方或者右方点 -  最大的下方或者左方点), 相乘就是overlap的面积
  4. 相减得到结果
public class Solution {
public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
int totalArea = (C - A) * (D - B) + (G - E) * (H - F);
if (A >= G || C <= E || B >= H || D <= F) {
return totalArea;
}
int overlap = (Math.min(C, G) - Math.max(A, E)) * (Math.min(D, H) - Math.max(B, F));
return totalArea - overlap;
}
}

Reference:

https://leetcode.com/discuss/39188/an-easy-to-understand-solution-in-java

https://leetcode.com/discuss/39398/my-java-solution-sum-of-areas-overlapped-area

https://leetcode.com/discuss/43549/just-another-short-way

https://leetcode.com/discuss/43173/if-you-want-to-laugh-look-at-my-solution

https://leetcode.com/discuss/54138/python-concise-solution

https://leetcode.com/discuss/51354/an-explanation-in-plain-language

http://www.cnblogs.com/0001/archive/2010/05/04/1726905.html

http://www.geeksforgeeks.org/find-two-rectangles-overlap/

https://www.cs.princeton.edu/~rs/AlgsDS07/17GeometricSearch.pdf

223. Rectangle Area的更多相关文章

  1. [LeetCode] 223. Rectangle Area 矩形面积

    Find the total area covered by two rectilinearrectangles in a 2D plane. Each rectangle is defined by ...

  2. 【刷题-LeetCode】223. Rectangle Area

    Rectangle Area Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectang ...

  3. Java for LeetCode 223 Rectangle Area

    Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined b ...

  4. (easy)LeetCode 223.Rectangle Area

    Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined b ...

  5. 【LeetCode】223 - Rectangle Area

    Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined b ...

  6. Java [Leetcode 223]Rectangle Area

    题目描述: Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is def ...

  7. LeetCode OJ 223.Rectangle Area

    Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined b ...

  8. 【一天一道LeetCode】#223. Rectangle Area

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Find th ...

  9. 【LeetCode】223. Rectangle Area 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/rectangl ...

随机推荐

  1. Django模版进阶

    # -*- coding: utf-8 -*-from django.shortcuts import renderdef home(request): string = "测试" ...

  2. Google面试题及答案

    1. 村子里有100对夫妻,其中每个丈夫都瞒着自己的妻子偷情...村里的每个妻子都能立即发现除自己丈夫之外的其他男人是否偷情,唯独不知道她自己的丈夫到底有没有偷情.村里的规矩不容忍通奸.任何一个妻子, ...

  3. 快书包CEO徐智明反思:我犯下哪些错误

    新浪科技 刘璨 1月23日,快书包CEO徐智明在微博上公开“叫卖”快书包,在业内引起不小反响.这家创立于2010年要做“网上711”的创业公司,曾以独特的“一小时送达”服务在业内成为关注焦点. “如果 ...

  4. Entity Framework学习笔记(五)----Linq查询(2)---贪婪加载

    请注明转载地址:http://www.cnblogs.com/arhat 在上一章中,我们使用了Linq对Entity Framework进行了一个查询,但是通过学习我们却发现了懒加载给我来的性能上的 ...

  5. 用例图 UseCase Diagram

    从上面的用例图模型,我们可以大致了解用例图所描述的是什么.下面进行详细介绍. 用例图,即用来描述什么角色通过某某系统能做什么事情的图,用例图关注的是系统的外在表现,系统与人的交互,系统与其它系统的交互 ...

  6. WebAPi(selfhost)

    ASP.NET WebAPi(selfhost)之文件同步或异步上传   前言 前面我们讲过利用AngularJs上传到WebAPi中进行处理,同时我们在MVC系列中讲过文件上传,本文结合MVC+We ...

  7. cocos2dx中的CCRect的用法

    CCRect是cocos2dx中的一块矩形区域 常用于碰撞检测及渲染时限定纹理的大小 CCRect rect=spr.boundingBox(); CCRect rect=CCRect(0,0,spr ...

  8. Luence简单实现2

    上一篇是基于内存存储的,这次的例子是基于本地存储索引库. 上一次的代码稍微修改,代码如下: //创建词法分析器 Analyzer analyzer = new StandardAnalyzer(); ...

  9. 来吧,给你的Winform列表控件画个妆

    前言 以前看别人的控件好看只有羡慕的份:以前觉得控件重绘是个很复杂的东西:以前知道MSDN很全面很专业却一直没有好好用起来: 作为初级程序猿,不能原地踏步,来吧,让我们一起把 TreeView 美化一 ...

  10. 【系统Configmachine.config与自己的应用程序的App.config/Web.Config配置节点重复】解决方法

    自己的应用程序的App.config或Web.Config文件中与系统的C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Configmachine.co ...