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 ...
随机推荐
- Python-Day9 Paramiko模块/进程/线程/RabbitMQ队列
一.Paramiko模块 1.Paramiko安装 Python的目录下有个Scripts目录,cd到这个目录用这里面的pip命令(如果添加的环境变量可以在cmd直接输入命令):pip install ...
- C#——中文转化成拼音
在KS系统中用到了中文转化成拼音的功能.通过查阅资料为下面是代码. /// <summary> /// MyConvert 的摘要说明 /// </summary> publi ...
- MySQL 多实例给root用户创建密码
DB:5.5.14 OS:CentOS 6.3 安装多实例MySQL数据库,安装完成后默认无密码,一次性给所有实例的root账户创建密码: #!/bin/bash for i in {3361..3 ...
- iOS中使用自定义字体
1.确定你的项目工程的Resources下有你要用的字体文件(.ttf或者.odf). 2.然后在你的工程的Info.plist文件中新建一行,添加key为:UIAppFonts,类型为Array或D ...
- Asp.Net生命周期系列五
如果您看了我的前四篇文章,应该知道目前Http请求已经流到了HttpModule这个程序员手中了,而且我们可以注册自己的HttpModule并且可以在里面注册一些事件来控制这个Http请求,但是到目前 ...
- GitHub error “Failed to get HEAD”
cd /要提交的文件的文件夹下 比如要提交一个名为 demo的 程序, 那么先进入demo 的文件夹里面 然后 进行以下两步 git init (有时这个是不必要的,因为xcode 自 ...
- .Xresources 配置文件
安装rxvt-unicode-256color,如果不是这个版本的话VIM配色会显示不正常. ~/.Xresources配置文件如下 !urxvt color scheme: URxvt*backgr ...
- Jquery 固定悬浮层以及固定表头
/* =========================================================== * jquery.autofix_anything.js v1 * === ...
- android控件---spinner
spinner下拉列表框的列表项有两种配置方式: 1.通过资源文件配置,通过在values种的xml,比如strings.xml中使用<string-array>元素添加制定列表项内容,然 ...
- CSS function--功能样式
功能样式,从常用样式方法中抽离,按需使用,使用前请先阅读 CSS规范 中相关条列. /* function */ .f-cb:after,.f-cbli li:after{display:block; ...