leetcode 223
题目描述:
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.
第一版代码:
思路就是先找是否有重叠区域,然后用两个矩形面积之和减去重叠区域,由于没有使用math库中的函数,导致代码显得臃肿,而且一开始写的时候各种情况没考虑完全,比如一个矩形完全包围另一个矩形的情况是提交之后发现WA之后才改的:
int computeArea(int A, int B, int C, int D, int E, int F, int G, int H)
{
int overlap_area = ;
if(!(G < A || C < E) && !(H < B || D < F))
{
int a = , b = ;
if(E < A && C < G)
a = C - A;
else if(A < E && G < C)
a = G - E;
else
a = G-A > C-E ? C-E : G-A; if(B < F && H < D)
b = H - F;
else if(F < B && D < H)
b = D - B;
else
b = H-B > D-F ? D-F : H-B;
overlap_area = a * b;
}
int area = (C - A ) * (D - B) - overlap_area + (G - E) * (H - F);
return area;
}
第二个版本的代码是看他人的博客的:
int computeArea(int A, int B, int C, int D, int E, int F, int G, int H)
{
int overlap_area = ( min(C, G) - max(A, E) ) * (min(D, H) - max(B, F));
if(min(C, G) < max(A, E) || (min(D, H) < max(B, F)))
overlap_area = ;
return (C-A) * (D-B) - overlap_area + (G - E) * (H - F);
}
这里使用了math库中的函数,非常好用,代码看上去非常简洁,虽然比起第一版的代码运行效率降低了一半,但是很简洁,一旦理解了就不容易出错,这才是好代码!
leetcode 223的更多相关文章
- [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)
223. 矩形面积 223. Rectangle Area 题目描述 在二维平面上计算出两个由直线构成的矩形重叠后形成的总面积. 每个矩形由其左下顶点和右上顶点坐标表示,如图所示. LeetCode2 ...
- Java实现 LeetCode 223 矩形面积
223. 矩形面积 在二维平面上计算出两个由直线构成的矩形重叠后形成的总面积. 每个矩形由其左下顶点和右上顶点坐标表示,如图所示. Rectangle Area 示例: 输入: -3, 0, 3, 4 ...
- Java for 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
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABRQAAAQ0CAYAAAAPPZBqAAAMFGlDQ1BJQ0MgUHJvZmlsZQAASImVlw
- (easy)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 223 Rectangle Area(矩形面积)
翻译 找到在二维平面中两个相交矩形的总面积. 每一个矩形都定义了其左下角和右上角的坐标. (矩形例如以下图) 如果,总占地面积永远不会超过int的最大值. 原文 分析 这题前天试过,写了一堆推断.终究 ...
- [LeetCode] 223.矩形面积
题目链接: https://leetcode-cn.com/problems/rectangle-area 难度:中等 通过率:41.3% 题目描述: 在 二维 平面上计算出两个 由直线构成的 矩形重 ...
随机推荐
- BZOJ2330 糖果题解 查分约束
BZOJ 2330 糖果题解 差分约束系统 + SPFA 题目传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2330 Description ...
- mysql 存储过程,搞死人的语法
MySQL 真心不如sqlserver灵活 存储过程注意事项: 1.declare 依次声明 DECLARE MyAccountID VARCHAR (36); DECLARE Balance DEC ...
- CentOS评估磁盘I/O性能读写极限测试
用一个fio工具 安装 yum -y install fio 二,FIO用法: 随机读:fio -direct=1 -iodepth 1 -thread -rw=randread -ioengine ...
- PHP 下的SSL加密设置
这个是报的错[Composer\Downloader\TransportException] The . OpenSSL Error messages: error::SSL routines:SSL ...
- e.Handled的理解
private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) { ...
- 最新Velocity使用和Velocity语法
Velocity语法 Velocity的使用要用到下面几个包,可以从官网下载,commons-collections.jar,velocity-1.4.jar,velocity-dept.jar; 1 ...
- light oj 1047 - Neighbor House 动态规划
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730#problem/C 题目: Description The peopl ...
- 在Apache中使用mod_rewrite模块重写URL
如果有使用第三方框架做项目时,url路径是可以同过框架给的方法来设定的(如thinkphp),但如果使用原生php写的项目又想重写url,则可通过apache的一些设置来达到想要的效果. 在更改apa ...
- sql*loader的直接加载方式和传统加载方式的性能差异
1.确认数据库版本 2.数据准备 3.创建导入表及控制文件 4.直接加载方式演示 查看具体的日志: 5.传统加载方式演示 查看日志文件: 6.结论及两种方式的差异 经过比对direct比convent ...
- NLP用CNN分类Mnist,提取出来的特征训练SVM及Keras的使用(demo)
用CNN分类Mnist http://www.bubuko.com/infodetail-777299.html /DeepLearning Tutorials/keras_usage 提取出来的特征 ...