223. Rectangle Area
题目:
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:
- 一开始先计算出两个矩形的面积和 totalArea
- 判断两个矩形是否相交,假如不相交,或者仅有顶点相交,那么我们直接返回totalArea。 这里两个矩形 x 的范围是 (A, C), (E, F), y的范围是(B, D), (E, F)
- 计算overlap的面积,边的计算公式是 ( 最小的上方或者右方点 - 最大的下方或者左方点), 相乘就是overlap的面积
- 相减得到结果
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的更多相关文章
- [LeetCode] 223. Rectangle Area 矩形面积
Find the total area covered by two rectilinearrectangles in a 2D plane. Each rectangle is defined by ...
- 【刷题-LeetCode】223. Rectangle Area
Rectangle Area Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectang ...
- Java for LeetCode 223 Rectangle Area
Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined b ...
- (easy)LeetCode 223.Rectangle Area
Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined b ...
- 【LeetCode】223 - Rectangle Area
Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined b ...
- Java [Leetcode 223]Rectangle Area
题目描述: Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is def ...
- LeetCode OJ 223.Rectangle Area
Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined b ...
- 【一天一道LeetCode】#223. Rectangle Area
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Find th ...
- 【LeetCode】223. Rectangle Area 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/rectangl ...
随机推荐
- Mac开发利器之程序员编辑器MacVim学习总结
Emacs和Vim都是程序员专用编辑器,Emacs被称为神的编辑器,Vim则是编辑器之神.至于两者到底哪个更好用,网络上两大派系至今还争论不休.不过,相比之下,Emacs更加复杂,已经不能算是一个编辑 ...
- google查询技巧
技巧一:使用正确的方法 无论你是使用一个简单或是高级的Google搜索,在此都存在你应该使用的某种可靠的方法.遵循适当的方法你就能获得非常准确的结果:要是忽略这条建议的话,你也许就会看到大量不相关的结 ...
- 修改UI中的值,无反应解决办法
var targetObj = $("<input name='mydate' class='easyui-datebox'>").appendTo("#id ...
- Nginx+Tomcat动静分离
需求:nginx处理用户请求的静态页面,tomcat处理用户请求jsp页面,来实现动态分离,nginx处理静态页面效率远高于tomcat,这样一来就能更好的提高并发,处理性能. 准备软件: 下载jdk ...
- Ztack学习笔记(3)-系统启动分析
一 系统启动 //OSAL.cvoid osal_start_system( void ) { #if !defined ( ZBIT ) && !defined ( UBIT ) f ...
- About Curah
相信下列场景对您来说一点都不陌生:您遇到一个问题,花了好几个小时在网上搜寻解答和可靠的技术内容.即使前往许多技术博客和论坛翻箱倒柜后,还是无法确定要相信谁,也不知道该选哪个答案. Curah! 网站就 ...
- 对现有Hive的大表进行动态分区
分区是在处理大型事实表时常用的方法.分区的好处在于缩小查询扫描范围,从而提高速度.分区分为两种:静态分区static partition和动态分区dynamic partition.静态分区和动态分区 ...
- Win7任务计划自由预设系统定时自动关机
大家在使用电脑的时候可能会遇到一些需要无人值守让电脑自行执行任务后定时关机的情形,在Win7系统中,我们可以使用"任务计划"设置功能结合shutdown命令灵活设置任务计划,让Wi ...
- java并发编程(一)
多个线程访问同一个变量时,可能会出现问题.这里我用两个线程同时访问一个int count变量,让他们同时+1.同时让线程睡眠1秒,每个线程执行10次,最后应该输出20才对,因为count++并不是原子 ...
- android 使某个控件获取焦点
比如让某个edittext获取焦点,可以调用edittext.requestfocuse()的方法