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% 题目描述: 在 二维 平面上计算出两个 由直线构成的 矩形重 ...
随机推荐
- HDU 4750 Count The Pairs(并查集)
题目链接 没有发现那个点,无奈. #include <cstdio> #include <cstring> #include <cmath> #include &l ...
- CSS+DIV两栏式全屏布局
在网上找了很久,发现大部分都是固定宽度设置两栏,全屏情况下的布局很少.最后终于完成了,写出来备查,也供大家参考. <!DOCTYPE html PUBLIC "-//W3C//DTD ...
- C++ string 类的 find 方法实例详解
1.C++ 中 string 类的 find 方法列表 size_type std::basic_string::find(const basic_string &__str, size_ty ...
- winform让子窗体始终居于父窗体的中间
实例: FormMsg msg = new FormMsg(); msg.TopMost = True; msg.StarPosition = FormStarPosition.Centerparen ...
- org.openqa.selenium.StaleElementReferenceException
org.openqa.selenium.StaleElementReferenceException如何解啊.什么原因造成的,貌似有时会出现,有时不会出现
- 如何用bat批处理编译swf项目
平时用FB等IDE编译多模块的游戏项目时,除了添加移除模块的操作很繁琐外,编译速度也非常之慢.而用bat来编译swf项目,速度非常快,稳定. 在此分享自己工作用的bat,每次运行会重新编译主模块Gam ...
- 学习bootstrap心得
Bootstrap 使用了一些 HTML5 元素和 CSS 属性.为了让这些正常工作,您需要使用 HTML5 文档类型(Doctype). 因此,请在使用 Bootstrap 项目的开头包含下面的代码 ...
- sql 将字符串转换为表
我们常常碰到需要将用户输入之字符串依特定的分隔符转换为表的一个栏位,下面是我写的一个sql函数.如能灵活运用则用途广泛. -- ==================================== ...
- ZK dropEvent简单使用
前台(Drop.zul) <?page title="拖动测试" contentType="text/html;charset=UTF-8"?> & ...
- springMVC+spring+hibernate注解上传文件到数据库,下载,多文件上传
数据库 CREATE TABLE `annex` ( `id` bigint() NOT NULL AUTO_INCREMENT, `realName` varchar() DEFAULT NULL, ...