题目描述:

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的更多相关文章

  1. [LeetCode] 223. Rectangle Area 矩形面积

    Find the total area covered by two rectilinearrectangles in a 2D plane. Each rectangle is defined by ...

  2. LeetCode 223. 矩形面积(Rectangle Area)

    223. 矩形面积 223. Rectangle Area 题目描述 在二维平面上计算出两个由直线构成的矩形重叠后形成的总面积. 每个矩形由其左下顶点和右上顶点坐标表示,如图所示. LeetCode2 ...

  3. Java实现 LeetCode 223 矩形面积

    223. 矩形面积 在二维平面上计算出两个由直线构成的矩形重叠后形成的总面积. 每个矩形由其左下顶点和右上顶点坐标表示,如图所示. Rectangle Area 示例: 输入: -3, 0, 3, 4 ...

  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 ...

  5. LeetCode : 223. Rectangle Area

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABRQAAAQ0CAYAAAAPPZBqAAAMFGlDQ1BJQ0MgUHJvZmlsZQAASImVlw

  6. (easy)LeetCode 223.Rectangle Area

    Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined b ...

  7. Java [Leetcode 223]Rectangle Area

    题目描述: Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is def ...

  8. LeetCode 223 Rectangle Area(矩形面积)

    翻译 找到在二维平面中两个相交矩形的总面积. 每一个矩形都定义了其左下角和右上角的坐标. (矩形例如以下图) 如果,总占地面积永远不会超过int的最大值. 原文 分析 这题前天试过,写了一堆推断.终究 ...

  9. [LeetCode] 223.矩形面积

    题目链接: https://leetcode-cn.com/problems/rectangle-area 难度:中等 通过率:41.3% 题目描述: 在 二维 平面上计算出两个 由直线构成的 矩形重 ...

随机推荐

  1. Android -- ids.xml文件的使用

    1.当我们在使用控件的时候,有的时候不可避免的要为控件声明id ,而分散在各个xml文件中,有时候查找起来又不是特别的方便 ,因此,安卓为我们提供了ids.xml 文件,保存在res -->va ...

  2. dataTransfer.getData()在dragover,dragenter,dragleave中无法获取数据的问题

    做拖拽相关效果时,想在ondragover时给被拖拽元素添加一些样式,于是在dragover事件的函数中通过dataTransfer.getData()获取在dragstart中设置的数据,然而发现d ...

  3. [转]AS3的垃圾回收

    GC和内存泄露无关 垃圾回收,这次是一个被无数人讨论过的传统话题. Action Script使用的是和Java相似的内存管理机制,并不会即时回收废弃对象的内存,而是在特定时间统一执行一次GC(Gab ...

  4. 快速安装zabbix agent并部署监控

    1.准备yum源: epel源:yum install -y zabbix22-agent 2.上传脚本: 上传脚本事先写好的监控脚本到/script/下面 3.修改配置文件:Server=10.10 ...

  5. HDU2563 递推

    统计问题 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  6. 使用 jQuery 页面回到顶部

    function backTop() { $(window).scroll(function () { if ($(window).scrollTop() > 100) { $("#t ...

  7. Linux关闭休眠和屏保模式

    本人因为特殊需求,想让某台Linux主机始终显示某个程序,显示器不能关机或者休眠或进入屏保模式. 环境:Ubuntu 11.10 最小化模式安装并安装有轻量级桌面openbox(非gnome).因为X ...

  8. rename 快速移动文件或者文件夹

    有几种情况: 1.对于文件,rename可以在不同盘符之间移动. 2.对于空文件夹,rename也可以在不同盘符之间移动.但是目标文件夹的父目录必须存在. 3.对于非空文件夹,只能在同一盘符下移动. ...

  9. mysql入门教程

    mysql相信大家都非常熟悉,but读音你们都读对了么?MySQL [maɪ ˌɛskjuːˈɛl] [maɪ ˈsiːkwəl] 念 买S奎儿 或 买吸扣 都可以,还有好多容易读错的名词,详情请见I ...

  10. stdout( 标准输出流)和 stderr( 标准输入流) 重定向问题

    我想把提示信息和错误信息列表都写入到同一个文件里, /* ** 设置流, 使输出与错误流都指向指定的文件 */ if( (output1 = freopen( PATH, "a", ...