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 ...
随机推荐
- Use a layout_width of 0dip instead of wrap_content for better performance.......【Written By KillerLegend】
当你在一个Linearlayout布局中只为一个组件设置android:layout_weight属性时,那么这个组件将默认填充Linearlayout的剩余空间(宽度与高度方向),而不用事先进行测量 ...
- malloc函数
C语言中,使用malloc函数向内存中动态申请空间. 函数的原型是extern void *malloc(unsigned int num_bytes); 可见,函数返回的是指针类型,参数是要申请的空 ...
- Oracle中的日期
--1.日期字符转换函数的用法 /****************************TO_CHAR********************************/ -------------- ...
- Entity Framework学习笔记(六)----使用Lambda查询Entity Framework(1)
请注明转载地址:http://www.cnblogs.com/arhat 在前几章中,老魏一直使用Linq来查询Entity Framework.但是老魏感觉,如果使用Linq的话,那么Linq的返回 ...
- UI控件tag属性和魔法数字的处理
说明:tag属性有很大的用处,它就好像每个UI控件的id,当多个按钮指向同一个监听方法时,可以给方法带参数UIButton,然后根据不同的tag值 来判断执行哪个按钮的监听事件: - (IBActio ...
- mysql 的日志文件
mysql的日志文件 日志文件大致分为 error log, binary log, query log, slow query log, innodb redo log ;如图: 1.error ...
- bzoj 4010: [HNOI2015]菜肴制作 拓扑排序
题目链接: 题目 4010: [HNOI2015]菜肴制作 Time Limit: 5 Sec Memory Limit: 512 MB 问题描述 知名美食家小 A被邀请至ATM 大酒店,为其品评菜肴 ...
- eclipse中加放js文件报js语法错误解决办法
1) eclipse设置 window->preference-> JavaScript -> Validator->Errors/Warnings->E ...
- 详解HTML5中的<aside>元素与<article>元素
<aside>元素HTML<aside>元素表示一个页面的一部分, 它的内容跟这个页面的其它内容的关联性不强,或者是没有关联,单独存在.<aside>元素通常显示成 ...
- 新建android项目src和layout文件夹中没有内容的问题
这个问题好像是最新版ADT(ver:23.0.0)才会出现的问题,解决办法也简单,直接把android SDK和ADT的老版本(ver:22.6.2)覆盖安装一次就好了.至于新版为什么会这么设计,现在 ...