题目:

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. Leet Code OJ 223. Rectangle Area [Difficulty: Easy]

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

  2. leetcode 223. Rectangle Area | 223. 矩形面积(Java)

    题目 https://leetcode.com/problems/rectangle-area/ 类似问题:蓝桥杯 BASIC-18 基础练习 矩形面积交 题解 本题的重点在于如何计算重叠部分的面积, ...

  3. LeetCode——223. 矩形面积(Rectangle Area)[中等]——分析及代码(C++)

    LeetCode--223. 矩形面积[Rectangle Area][中等]--分析及代码[C++] 一.题目 二.分析及代码 1. 几何计算 (1)思路 (2)代码 (3)结果 三.其他 一.题目 ...

  4. LeetCode 850. Rectangle Area II

    题目地址: We are given a list of (axis-aligned) rectangles. Each rectangle[i] = [x1, y1, x2, y2] , where ...

  5. Leetcode题目:Rectangle Area

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

  6. Rectangle Area

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

  7. leetcode 850. Rectangle Area II | 850. 矩形面积 II(递归分割未重叠矩形)

    题目 https://leetcode.com/problems/rectangle-area-ii/ 题解 没有看懂官方答案,评论区有一种解法写的挺通俗的: Clean Recursive Solu ...

  8. LeetCode Rectangle Area

    题意:给出两个矩形,求其面积和 思路:两个矩形可能相交,在求相交面积时,左下角取两个矩形左下角的最大值 ,右上角取两个矩形对应坐标的最小值 ,如果求得的左下角大于右下角,说明不相交,面积为两个矩形面积 ...

  9. 继续过中等难度.0309

      .   8  String to Integer (atoi)    13.9% Medium   . 151 Reverse Words in a String      15.7% Mediu ...

最新文章

  1. 10G_Ethernet_02 10G Ethernet Subsystem 简介
  2. 第六篇: 分布式配置中心(Spring Cloud Config)(Finchley版本)V2.0_dev
  3. 对老赖 绝不要忍 !一位美女程序媛的讨薪经历...
  4. SpringBoot防XSS攻击
  5. 今日恐慌与贪婪指数为78 贪婪程度有所上升
  6. matlab拟合出余弦曲线,如何用matlab做正弦曲线拟合?
  7. 《Java并发编程实践》笔记1——并发编程基础
  8. Lua代码加密 LuaJit代码加密
  9. Java计算同比环比
  10. uniapp打开pdf文件
  11. 北京的三甲医院都是定点医院吗?不列入医保卡范围不能报销?
  12. 【翻译】Paparazzi: Surface Editing by way of Multi-View Image Processing
  13. Elasticsearch:Standard Text Analyzer - 标准文本分析器
  14. Apache2 Windows安装与HTTP Server Digest 认证
  15. 物联网是如何工作的?
  16. 当你觉得生活快熬不下去时,请你读一读《活着》
  17. 《面向对象程序设计》课程设计报告
  18. 微信小程序 自定义组件之 胶囊对齐 搜索FloatSearch
  19. 基于simulink的PID控制器设计
  20. 3DMAX机械建模贴图教程

热门文章

  1. redis安装后提示权限问题ERR operation not permitted
  2. QTableView中点击单元格弹出QComboBox
  3. Java网络编程之TCP、UDP
  4. 应用更新iOS 开发:应用内实现 更新提醒
  5. Ubuntu8.10安装Netbeans6.7中文乱码解决方案
  6. 如何去遍历对象中的所有的属性值
  7. mysql 并行复制搭建_基于GTID的主从实践系列之④并行复制搭建及测试
  8. java 回车 按钮事件,java--键盘事件类,按下回车则模拟鼠标
  9. leetcode算法题--删除排序链表中的重复元素 II
  10. 汇编quad_汇编语言中的英文缩写