题目:

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. 通过获取客户端Json数据字符串,反序列化为实体对象的一段代码

    #region 保存候选人数据 /// <summary> /// 保存候选人数据 /// </summary> /// <param name="entity ...

  2. Python 学习教程

    <Core Python Programming>勘误参考表 http://starship.python.net/crew/wesc/cpp/errata2.htm 笨办法学 Pytho ...

  3. Struts 2简单配置分析

    要配置Struts 2,首先先要有Struts 2的Jar包,可以去Struts的官网下载(http://struts.apache.org/),这里有3个GA版本可以选择下载,我选择的是最新的2.2 ...

  4. Oracle收缩表空间

    可以使用 alter database datafile 'file path...' resize xM 的命令来缩小数据文件. SELECT 'alter database datafile '' ...

  5. .NET序员的成长之路

  6. Android存储机制之Preference

    Preference提供了一种轻量级的数据存取方法,主要是数据比较少的配置信息.它以键值对的方式将数据保存在一个XML配置文件中. 使用Preference方式来存取数据,用到了SharedPrefe ...

  7. topcoder 673

    DiV1 300:给一组士兵再给一组战马都有权值. 安排战马的顺序的方案数,是第一个士兵和其战马的权值乘积最大. 做法:随便暴力就好. 枚举战马和第一个士兵匹配.其他士兵按权值从大到小排序,战马权值按 ...

  8. VB数据库经典实例总结(一)

    先让大家看一张图.随后讲解..... 敲完五个例子之后的心情是非常好的.并没有想象中的那么难,深究它的话大致思路就是.: 建立数据库 --->利用VB导出数据 --->供人们使用.. 因为 ...

  9. learning from the previous teams

    开发人员水平有限.分配任务的时候经常有说这个事儿做不到,或者压根不知道怎么做:验收工作频出意外,DEV写了一个模块之后,验收的时候发现模块质量不行,代码质量低是其次,无法按照给定的接口工作.设计不足. ...

  10. Ext学习-前后交互模式介绍

    在前后台交互模式的介绍中,实际上就是Store中Proxy相关的内容,比如Ajax提交. 所以详细的文档请参考: Ext学习-基础概念,核心思想介绍   中关于数据模型和MVC结构部分. 作者:sdj ...